Count businesses per location and sort by count, then by location name

Quick Overview

Given parallel arrays of business names, locations and IDs, return each location with the number of businesses in it, ordered by count descending and then by location name. It tests hash map counting and sorting with a compound key that mixes descending and ascending order.

Count businesses per location and sort by count, then by location name

Company: Yelp

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given three parallel arrays describing a list of businesses: `names`, `locations` and `ids`. The i-th business has name `names[i]`, location `locations[i]` and ID `ids[i]`. Return a list of pairs `(location, count)` giving the number of businesses in each unique location. The output must be sorted first by count in descending order, and then alphabetically by location name (ascending) to break ties. ### Function Signature ```python def location_business_count(names: list[str], locations: list[str], ids: list[int]) -> list[list]: ``` ### Rules - Each pair in the result is a two-element list `[location, count]`. - Locations are compared exactly as given, so two strings are the same location only when they are identical. - Ties on count are broken by ordinary string comparison of the location names (by character code, as Python compares strings). ### Constraints - `1 <= len(names) == len(locations) == len(ids) <= 10^5` - `1 <= len(names[i]), len(locations[i]) <= 100` - All values in `ids` are unique. ### Examples ```text Input: names = ["A", "B", "C", "D", "E", "F"], locations = ["NYC", "LA", "NYC", "Chicago", "LA", "NYC"], ids = [1, 2, 3, 4, 5, 6] Output: [["NYC", 3], ["LA", 2], ["Chicago", 1]] ``` NYC appears 3 times, LA 2 times and Chicago once. The counts are all different, so sorting by count descending fixes the order. ```text Input: names = ["A", "B", "C", "D"], locations = ["Boston", "Austin", "Boston", "Austin"], ids = [10, 20, 30, 40] Output: [["Austin", 2], ["Boston", 2]] ``` Boston and Austin both have count 2. The tie is broken alphabetically, so Austin comes before Boston even though Boston appears first in the input. ```text Input: names = ["A", "B", "C", "D", "E", "F"], locations = ["Seattle", "Austin", "Seattle", "Boston", "Austin", "Denver"], ids = [1, 2, 3, 4, 5, 6] Output: [["Austin", 2], ["Seattle", 2], ["Boston", 1], ["Denver", 1]] ``` Austin and Seattle have 2 businesses each and come first, in alphabetical order. Boston and Denver have 1 each and follow, also in alphabetical order.

Overview: Given parallel arrays of business names, locations and IDs, return each location with the number of businesses in it, ordered by count descending and then by location name. It tests hash map counting and sorting with a compound key that mixes descending and ascending order.

Yelp logo
Yelp
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given three parallel arrays describing a list of businesses: names, locations and ids. The i-th business has name names[i], location locations[i] and ID ids[i].

Return a list of pairs (location, count) giving the number of businesses in each unique location.

The output must be sorted first by count in descending order, and then alphabetically by location name (ascending) to break ties.

Function Signature

def location_business_count(names: list[str], locations: list[str], ids: list[int]) -> list[list]:

Rules

  • Each pair in the result is a two-element list [location, count] .
  • Locations are compared exactly as given, so two strings are the same location only when they are identical.
  • Ties on count are broken by ordinary string comparison of the location names (by character code, as Python compares strings).

Constraints

  • 1 <= len(names) == len(locations) == len(ids) <= 10^5
  • 1 <= len(names[i]), len(locations[i]) <= 100
  • All values in ids are unique.

Examples

Input: names = ["A", "B", "C", "D", "E", "F"], locations = ["NYC", "LA", "NYC", "Chicago", "LA", "NYC"], ids = [1, 2, 3, 4, 5, 6]
Output: [["NYC", 3], ["LA", 2], ["Chicago", 1]]

NYC appears 3 times, LA 2 times and Chicago once. The counts are all different, so sorting by count descending fixes the order.

Input: names = ["A", "B", "C", "D"], locations = ["Boston", "Austin", "Boston", "Austin"], ids = [10, 20, 30, 40]
Output: [["Austin", 2], ["Boston", 2]]

Boston and Austin both have count 2. The tie is broken alphabetically, so Austin comes before Boston even though Boston appears first in the input.

Input: names = ["A", "B", "C", "D", "E", "F"], locations = ["Seattle", "Austin", "Seattle", "Boston", "Austin", "Denver"], ids = [1, 2, 3, 4, 5, 6]
Output: [["Austin", 2], ["Seattle", 2], ["Boston", 1], ["Denver", 1]]

Austin and Seattle have 2 businesses each and come first, in alphabetical order. Boston and Denver have 1 each and follow, also in alphabetical order.