Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
A scenic location is represented by its name and attractiveness score, where name is a unique string among all locations and score is an integer. Locations can be ranked from the best to the worst. The higher the score, the better the location. If the scores of two locations are equal, then the location with the lexicographically smaller name is better.
You are building a system that tracks the ranking of locations with the system initially starting with no locations. It supports:
ith best location of all locations already added, where i is the number of times the system has been queried (including the current query).
4th time, it returns the 4th best location of all locations already added.Note that the test data are generated so that at any time, the number of queries does not exceed the number of locations added to the system.
Implement the SORTracker class:
SORTracker() Initializes the tracker system.void add(string name, int score) Adds a scenic location with name and score to the system.string get() Queries and returns the ith best location, where i is the number of times this method has been invoked (including this invocation).Example 1:
Input
["SORTracker", "add", "add", "get", "add", "get", "add", "get", "add", "get", "add", "get", "get"]
[[], ["bradford", 2], ["branford", 3], [], ["alps", 2], [], ["orland", 2], [], ["orlando", 3], [], ["alpine", 2], [], []]
Output
[null, null, null, "branford", null, "alps", null, "bradford", null, "bradford", null, "bradford", "orland"]
Explanation
SORTracker tracker = new SORTracker(); // Initialize the tracker system.
tracker.add("bradford", 2); // Add location with name="bradford" and score=2 to the system.
tracker.add("branford", 3); // Add location with name="branford" and score=3 to the system.
tracker.get(); // The sorted locations, from best to worst, are: branford, bradford.
// Note that branford precedes bradford due to its higher score (3 > 2).
// This is the 1st time get() is called, so return the best location: "branford".
tracker.add("alps", 2); // Add location with name="alps" and score=2 to the system.
tracker.get(); // Sorted locations: branford, alps, bradford.
// Note that alps precedes bradford even though they have the same score (2).
// This is because "alps" is lexicographically smaller than "bradford".
// Return the 2nd best location "alps", as it is the 2nd time get() is called.
tracker.add("orland", 2); // Add location with name="orland" and score=2 to the system.
tracker.get(); // Sorted locations: branford, alps, bradford, orland.
// Return "bradford", as it is the 3rd time get() is called.
tracker.add("orlando", 3); // Add location with name="orlando" and score=3 to the system.
tracker.get(); // Sorted locations: branford, orlando, alps, bradford, orland.
// Return "bradford".
tracker.add("alpine", 2); // Add location with name="alpine" and score=2 to the system.
tracker.get(); // Sorted locations: branford, orlando, alpine, alps, bradford, orland.
// Return "bradford".
tracker.get(); // Sorted locations: branford, orlando, alpine, alps, bradford, orland.
// Return "orland".
Constraints:
name consists of lowercase English letters, and is unique among all locations.1 <= name.length <= 101 <= score <= 105get does not exceed the number of calls to add.4 * 104 calls in total will be made to add and get.Problem summary: A scenic location is represented by its name and attractiveness score, where name is a unique string among all locations and score is an integer. Locations can be ranked from the best to the worst. The higher the score, the better the location. If the scores of two locations are equal, then the location with the lexicographically smaller name is better. You are building a system that tracks the ranking of locations with the system initially starting with no locations. It supports: Adding scenic locations, one at a time. Querying the ith best location of all locations already added, where i is the number of times the system has been queried (including the current query). For example, when the system is queried for the 4th time, it returns the 4th best location of all locations already added. Note that the test data are generated so that at any time, the number of queries does not exceed
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Design · Segment Tree
["SORTracker","add","add","get","add","get","add","get","add","get","add","get","get"] [[],["bradford",2],["branford",3],[],["alps",2],[],["orland",2],[],["orlando",3],[],["alpine",2],[],[]]
find-median-from-data-stream)kth-largest-element-in-a-stream)finding-mk-average)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2102: Sequentially Ordinal Rank Tracker
class SORTracker {
private PriorityQueue<Map.Entry<Integer, String>> good = new PriorityQueue<>(
(a, b)
-> a.getKey().equals(b.getKey()) ? b.getValue().compareTo(a.getValue())
: a.getKey() - b.getKey());
private PriorityQueue<Map.Entry<Integer, String>> bad = new PriorityQueue<>(
(a, b)
-> a.getKey().equals(b.getKey()) ? a.getValue().compareTo(b.getValue())
: b.getKey() - a.getKey());
public SORTracker() {
}
public void add(String name, int score) {
good.offer(Map.entry(score, name));
bad.offer(good.poll());
}
public String get() {
good.offer(bad.poll());
return good.peek().getValue();
}
}
/**
* Your SORTracker object will be instantiated and called as such:
* SORTracker obj = new SORTracker();
* obj.add(name,score);
* String param_2 = obj.get();
*/
// Accepted solution for LeetCode #2102: Sequentially Ordinal Rank Tracker
// Auto-generated Go example from java.
func exampleSolution() {
}
// Reference (java):
// // Accepted solution for LeetCode #2102: Sequentially Ordinal Rank Tracker
// class SORTracker {
// private PriorityQueue<Map.Entry<Integer, String>> good = new PriorityQueue<>(
// (a, b)
// -> a.getKey().equals(b.getKey()) ? b.getValue().compareTo(a.getValue())
// : a.getKey() - b.getKey());
// private PriorityQueue<Map.Entry<Integer, String>> bad = new PriorityQueue<>(
// (a, b)
// -> a.getKey().equals(b.getKey()) ? a.getValue().compareTo(b.getValue())
// : b.getKey() - a.getKey());
//
// public SORTracker() {
// }
//
// public void add(String name, int score) {
// good.offer(Map.entry(score, name));
// bad.offer(good.poll());
// }
//
// public String get() {
// good.offer(bad.poll());
// return good.peek().getValue();
// }
// }
//
// /**
// * Your SORTracker object will be instantiated and called as such:
// * SORTracker obj = new SORTracker();
// * obj.add(name,score);
// * String param_2 = obj.get();
// */
# Accepted solution for LeetCode #2102: Sequentially Ordinal Rank Tracker
class SORTracker:
def __init__(self):
self.sl = SortedList()
self.i = -1
def add(self, name: str, score: int) -> None:
self.sl.add((-score, name))
def get(self) -> str:
self.i += 1
return self.sl[self.i][1]
# Your SORTracker object will be instantiated and called as such:
# obj = SORTracker()
# obj.add(name,score)
# param_2 = obj.get()
// Accepted solution for LeetCode #2102: Sequentially Ordinal Rank Tracker
/**
* [2102] Sequentially Ordinal Rank Tracker
*
* A scenic location is represented by its name and attractiveness score, where name is a unique string among all locations and score is an integer. Locations can be ranked from the best to the worst. The higher the score, the better the location. If the scores of two locations are equal, then the location with the lexicographically smaller name is better.
* You are building a system that tracks the ranking of locations with the system initially starting with no locations. It supports:
*
* Adding scenic locations, one at a time.
* Querying the i^th best location of all locations already added, where i is the number of times the system has been queried (including the current query).
*
* For example, when the system is queried for the 4^th time, it returns the 4^th best location of all locations already added.
*
*
*
* Note that the test data are generated so that at any time, the number of queries does not exceed the number of locations added to the system.
* Implement the SORTracker class:
*
* SORTracker() Initializes the tracker system.
* void add(string name, int score) Adds a scenic location with name and score to the system.
* string get() Queries and returns the i^th best location, where i is the number of times this method has been invoked (including this invocation).
*
*
* Example 1:
*
* Input
* ["SORTracker", "add", "add", "get", "add", "get", "add", "get", "add", "get", "add", "get", "get"]
* [[], ["bradford", 2], ["branford", 3], [], ["alps", 2], [], ["orland", 2], [], ["orlando", 3], [], ["alpine", 2], [], []]
* Output
* [null, null, null, "branford", null, "alps", null, "bradford", null, "bradford", null, "bradford", "orland"]
* Explanation
* SORTracker tracker = new SORTracker(); // Initialize the tracker system.
* tracker.add("bradford", 2); // Add location with name="bradford" and score=2 to the system.
* tracker.add("branford", 3); // Add location with name="branford" and score=3 to the system.
* tracker.get(); // The sorted locations, from best to worst, are: branford, bradford.
* // Note that branford precedes bradford due to its higher score (3 > 2).
* // This is the 1^st time get() is called, so return the best location: "branford".
* tracker.add("alps", 2); // Add location with name="alps" and score=2 to the system.
* tracker.get(); // Sorted locations: branford, alps, bradford.
* // Note that alps precedes bradford even though they have the same score (2).
* // This is because "alps" is lexicographically smaller than "bradford".
* // Return the 2^nd best location "alps", as it is the 2^nd time get() is called.
* tracker.add("orland", 2); // Add location with name="orland" and score=2 to the system.
* tracker.get(); // Sorted locations: branford, alps, bradford, orland.
* // Return "bradford", as it is the 3^rd time get() is called.
* tracker.add("orlando", 3); // Add location with name="orlando" and score=3 to the system.
* tracker.get(); // Sorted locations: branford, orlando, alps, bradford, orland.
* // Return "bradford".
* tracker.add("alpine", 2); // Add location with name="alpine" and score=2 to the system.
* tracker.get(); // Sorted locations: branford, orlando, alpine, alps, bradford, orland.
* // Return "bradford".
* tracker.get(); // Sorted locations: branford, orlando, alpine, alps, bradford, orland.
* // Return "orland".
*
*
* Constraints:
*
* name consists of lowercase English letters, and is unique among all locations.
* 1 <= name.length <= 10
* 1 <= score <= 10^5
* At any time, the number of calls to get does not exceed the number of calls to add.
* At most 4 * 10^4 calls in total will be made to add and get.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/sequentially-ordinal-rank-tracker/
// discuss: https://leetcode.com/problems/sequentially-ordinal-rank-tracker/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
struct SORTracker {
//
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl SORTracker {
fn new() -> Self {
Self {}
}
fn add(&self, name: String, score: i32) {
//
}
fn get(&self) -> String {
"".to_string()
}
}
/**
* Your SORTracker object will be instantiated and called as such:
* let obj = SORTracker::new();
* obj.add(name, score);
* let ret_2: String = obj.get();
*/
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2102_example_1() {
let mut tracker = SORTracker::new(); // Initialize the tracker system.
tracker.add("bradford".to_string(), 2); // Add location with name="bradford" and score=2 to the system.
tracker.add("branford".to_string(), 3); // Add location with name="branford" and score=3 to the system.
assert_eq!(tracker.get(), "branford".to_string()); // The sorted locations, from best to worst, are: branford, bradford.
// Note that branford precedes bradford due to its higher score (3 > 2).
// This is the 1^st time get() is called, so return the best location: "branford".
tracker.add("alps".to_string(), 2); // Add location with name="alps" and score=2 to the system.
assert_eq!(tracker.get(), "alps".to_string()); // Sorted locations: branford, alps, bradford.
// Note that alps precedes bradford even though they have the same score (2).
// This is because "alps" is lexicographically smaller than "bradford".
// Return the 2^nd best location "alps", as it is the 2^nd time get() is called.
tracker.add("orland".to_string(), 2); // Add location with name="orland" and score=2 to the system.
assert_eq!(tracker.get(), "bradford".to_string()); // Sorted locations: branford, alps, bradford, orland.
// Return "bradford", as it is the 3^rd time get() is called.
tracker.add("orlando".to_string(), 3); // Add location with name="orlando" and score=3 to the system.
assert_eq!(tracker.get(), "bradford".to_string()); // Sorted locations: branford, orlando, alps, bradford, orland.
// Return "bradford".
tracker.add("alpine".to_string(), 2); // Add location with name="alpine" and score=2 to the system.
assert_eq!(tracker.get(), "bradford".to_string()); // Sorted locations: branford, orlando, alpine, alps, bradford, orland.
// Return "bradford".
assert_eq!(tracker.get(), "orland".to_string()); // Sorted locations: branford, orlando, alpine, alps, bradford, orland.
// Return "orland".
}
}
// Accepted solution for LeetCode #2102: Sequentially Ordinal Rank Tracker
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2102: Sequentially Ordinal Rank Tracker
// class SORTracker {
// private PriorityQueue<Map.Entry<Integer, String>> good = new PriorityQueue<>(
// (a, b)
// -> a.getKey().equals(b.getKey()) ? b.getValue().compareTo(a.getValue())
// : a.getKey() - b.getKey());
// private PriorityQueue<Map.Entry<Integer, String>> bad = new PriorityQueue<>(
// (a, b)
// -> a.getKey().equals(b.getKey()) ? a.getValue().compareTo(b.getValue())
// : b.getKey() - a.getKey());
//
// public SORTracker() {
// }
//
// public void add(String name, int score) {
// good.offer(Map.entry(score, name));
// bad.offer(good.poll());
// }
//
// public String get() {
// good.offer(bad.poll());
// return good.peek().getValue();
// }
// }
//
// /**
// * Your SORTracker object will be instantiated and called as such:
// * SORTracker obj = new SORTracker();
// * obj.add(name,score);
// * String param_2 = obj.get();
// */
Use this to step through a reusable interview workflow for this problem.
Use a simple list or array for storage. Each operation (get, put, remove) requires a linear scan to find the target element — O(n) per operation. Space is O(n) to store the data. The linear search makes this impractical for frequent operations.
Design problems target O(1) amortized per operation by combining data structures (hash map + doubly-linked list for LRU, stack + min-tracking for MinStack). Space is always at least O(n) to store the data. The challenge is achieving constant-time operations through clever structure composition.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.