Sequentially Ordinal Rank Tracker - Problem
Design a Dynamic Location Ranking System

Imagine you're building a travel app that tracks scenic locations and their popularity in real-time! You need to create a SORTracker (Sequentially Ordinal Rank Tracker) system that manages location rankings dynamically.

How it works:
• Each scenic location has a name (unique string) and an attractiveness score (integer)
• Locations are ranked from best to worst: higher scores = better locations
• If two locations have the same score, the one with the lexicographically smaller name wins
• The system supports two operations:
  1. add(name, score) - Add a new location to the system
  2. get() - Return the ith best location, where i = number of times get() has been called

Example: First call to get() returns 1st best location, second call returns 2nd best, third call returns 3rd best, and so on.

Your task: Implement the SORTracker class that efficiently handles these operations while maintaining the correct ranking order.

Input & Output

example_1.py — Basic Usage
$ Input: ["SORTracker", "add", "add", "get", "add", "get", "add", "get", "add", "get", "get", "get"] [[], ["bradford", 2], ["branford", 3], [], ["alps", 2], [], ["orland", 2], [], ["orlando", 3], [], [], []]
Output: [null, null, null, "branford", null, "alps", null, "bradford", null, "bradford", "branford", "orlando"]
💡 Note: The tracker processes locations and queries sequentially. After adding bradford(2) and branford(3), the 1st query returns branford (highest score). After adding alps(2), the 2nd query returns alps (same score as bradford, but lexicographically smaller). The pattern continues with each query returning the i-th best location.
example_2.py — Tie Breaking
$ Input: ["SORTracker", "add", "add", "add", "get", "get", "get"] [[], ["zoo", 5], ["apple", 5], ["banana", 5], [], [], []]
Output: [null, null, null, null, "apple", "banana", "zoo"]
💡 Note: When all locations have the same score (5), tie-breaking is done lexicographically. The queries return locations in alphabetical order: apple, banana, zoo.
example_3.py — Mixed Scores
$ Input: ["SORTracker", "add", "get", "add", "get", "add", "get"] [[], ["mountain", 8], [], ["beach", 9], [], ["forest", 7], []]
Output: [null, null, "mountain", null, "beach", null, "mountain"]
💡 Note: First query returns mountain (only location). After adding beach(9), second query returns beach (highest score). After adding forest(7), third query returns mountain (second best after beach).

Constraints

  • 1 ≤ name.length ≤ 10
  • name consists of lowercase English letters
  • 1 ≤ score ≤ 106
  • At most 4 × 104 calls will be made to add and get
  • At any time, the number of calls to get does not exceed the number of calls to add

Visualization

Tap to expand
🏆 Winners Announced(Max Heap - Worst of Winners at Top)Movie CScore: 8.5Movie AScore: 9.2Movie BScore: 8.8🎬 Nominees Waiting(Min Heap - Best of Waiting at Top)Movie FScore: 7.9Movie DScore: 7.3Movie EScore: 6.8BoundaryNext Announcement: Movie C (8.5)🎯 Algorithm Steps:1. New movie nominated → Insert into waiting list2. Compare with boundary → Move best waiting to winners if needed3. Announce winner → Return worst of current winners4. Adjust boundary → Move next best waiting to winners for next time
Understanding the Visualization
1
Set Up Two Lists
Keep a 'Winners Announced' list (max heap) and 'Nominees Waiting' list (min heap)
2
New Nomination Arrives
Compare with current boundary and place in appropriate list
3
Announce Next Winner
Take from 'Winners Announced' list and adjust boundary
4
Rebalance Lists
Move top nominee to winners list to maintain correct boundary
Key Takeaway
🎯 Key Insight: By maintaining two heaps separated by a dynamic boundary, we can efficiently track the k-th best element in a streaming dataset without expensive sorting operations.
Asked in
Google 42 Amazon 38 Apple 35 Meta 29 Microsoft 24
43.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen