Median Home Price by City
Reported by candidates from Airbnb's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure is a hash map from city to a list of prices, and that's the whole Airbnb setup from the September 2026 reports. Parse each "city price" string, group prices by city, sort each group, then pull the median. It's a grouping problem dressed up as a median problem. With up to 2 * 10^5 entries, the plan is simple but the formatting trips people up. If you blank on the half-value output during the OA, StealthCoder is the invisible safety net that reads the screen and hands you a working solution.
The problem
Each entry is city price, where city has no spaces and price is a nonnegative integer. Return one city=median string per city, sorted by city. For an even count, use the mean of the two middle values. Medians are formatted as an integer or with a.5 suffix. Function cityPriceMedians(entries: String[]) → String[] Examples Example 1 entries = ["sf 10","ny 4","sf 20","ny 8","sf 30"] return = ["ny=6","sf=20"] Each city's prices are ordered independently. Example 2 entries = ["a 1","a 2"] return = ["a=1.5"] The average of the two middle integers may be a half. Example 3 entries = ["z 0"] return = ["z=0"] A singleton's value is its median. Constraints 1 <= entries.length <= 2 * 10^5. City names contain lowercase letters and prices fit signed 32-bit integers.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build a map of city to a list of integers. Split each entry on the space, parse the price, append. Then sort the city keys alphabetically and sort each price list. For odd counts, the median is the middle element. For even counts, add the two middle values and check parity. If the sum is even, print sum/2 as an integer. If it's odd, print sum/2 followed by.5, so use integer division and append the suffix. Never use floating point division here, since it risks printing 6.0 instead of 6. Use 64-bit math for the sum, because two 32-bit values can overflow a 32-bit int. The common pitfall is forgetting to sort the final output by city, or formatting 1.5 wrong. Total cost is O(n log n). If you freeze mid-OA, StealthCoder runs invisibly and gives you the pattern and code so you can finish cleanly.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Median Home Price by City cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Airbnb's OA.
Airbnb reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Median Home Price by City FAQ
How hard is the Airbnb city median problem really?+
Easy to medium. The algorithm is grouping plus sorting, nothing exotic. The difficulty is in details: parsing strings, sorting city names, handling even counts, and formatting the.5 suffix without floating point. If you've written a median function before, you'll finish it fast.
What's the trick to formatting the median?+
Skip floats entirely. Sum the two middle values as a 64-bit integer. If the sum is even, output sum/2. If it's odd, output sum/2 using integer division plus ".5". Since prices are nonnegative, integer division floors correctly and you avoid output like 6.0.
Do I need a heap or a running median structure?+
No. You only need the median once per city after reading all entries, so sorting each city's list is enough. Two heaps are for streaming medians, which this problem doesn't ask for. Sorting keeps the code short and the cost at O(n log n).
What edge cases should I test before submitting?+
Test a single entry, a city with exactly two prices that gives a.5 result, and two prices that sum to an even number. Also test cities that arrive out of alphabetical order and prices at the 32-bit maximum, where a naive sum could overflow.
How do I prepare for this in 48 hours?+
Practice the hash map of lists pattern, then write the median logic for odd and even lengths. Drill string splitting in your chosen language and sorting map keys. Run the three given examples plus an overflow case. That covers nearly everything this question tests.