'Time complexity of this mapbox json data
so I have been debating on what would the time complexity of this object response be when mapping through it. To be more precise, on a backend code I am mapping through features and before I was putting the context array into another data type like an object or a new array so that instead of looping through the context array inside of the for loop that loops through features, I would do it outside to avoid nested for loops.
To give even more context, the maximum amount of items that Mapbox geocoding returns is 10, and for context the maximum amount of items that it could hold is 9. Since I am not an expert in DS&A, I tend to think that having nested for loops will lead to n^2 unless the length is fixed. But in this case, if n is the amount of places that mapbox can return, and m is the amount of items inside of context, if I loop through each of the max 10 objects features array, and inside of that I loop through context(context can have a max length of 9) I believe that this will be O(n*m) since it is nested. If I were to add all of those context arrays for each of the 10 features, it will still result in 9 context arrays, but if I were to add it into an object and then map through the context array outside of the features array, wouldn't this be O(n+m) since it is not nested? And O(90) is better than O(19) but someone at work told me that mapping outside, so having two for loops separate from each other rather than one inside another on, is actually a worse time complexity, I believe they mentioned being O(n + n*m) which I don't see how that is possible.
As I said, I am not an expert on DS&A, but I think having two separate for loops will result in O(n+m)
{
type: "FeatureCollection"
query: [
0: "boston"
]
features: [
0: {
id: "place.9454360526012190"
type: "Feature"
place_type: [...
]
relevance: 1
properties: {...
}
text: "Boston"
place_name: "Boston, Massachusetts, United St ..."
bbox: [...
]
center: [...
]
geometry: {...
}
context: [
0: {
id: "district.11510878044935260"
wikidata: "Q54072"
text: "Suffolk County"
}
1: {
id: "region.8307399429561540"
short_code: "US-MA"
wikidata: "Q771"
text: "Massachusetts"
}
2: {
id: "country.19678805456372290"
wikidata: "Q30"
short_code: "us"
text: "United States"
}
]
}
1: {... }
2: {...}
3: {...}
4: {...}
]
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
