Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
City Ordering From Tuples
00:00
5 left

City Ordering From Tuples

MediumPython

Problem

Given tuples (population, number of cities to the left with population >= this one), reconstruct the correct order of the cities.

Asked in the Round 2 stage. The intended approach sorts tuples with a comparator, then inserts each tuple at its specified position.

Implement reconstruct_order(cities), where cities is a list of (population, count) tuples. Return a list containing the same tuples in an order where each tuple's count equals the number of earlier tuples whose population is greater than or equal to its population. Inputs are guaranteed to have at least one valid ordering.

Constraints

  • 1 <= len(cities) <= 1000
  • 0 <= population <= 10^9
  • 0 <= count < len(cities)
  • The input has at least one valid ordering

Function Signature

def reconstruct_order(cities):
Interviewer

Your question is City Ordering From Tuples. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.