Dynamic Kth Largest Queries
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The 2nd largest after inserting 4 and 9 into [1,3,7] is 7, and the 4th largest is 3. That's the whole shape of this Amazon OA question, reported in April 2026. Dynamic Kth Largest Queries gives you a starting list and a stream of insert and find operations, and k changes on every find. So the usual fixed-k min-heap trick doesn't work. You need an ordered structure that supports insertion and rank lookup. If you blank on the data structure during the live assessment, StealthCoder is the safety net running invisibly on your screen.
The problem
You are given an initial list of integer values and a stream of operations. The list changes over time as values are inserted. Each operation is one of the following: "insert": insert the accompanying value into the list. "find": treat the accompanying value as k and return the current k-th largest value in the list. Return the answers to all "find" operations in order. Function dynamicKthLargestQueries(initialValues: int[], operations: String[], values: int[]) → int[] Examples Example 1 initialValues = [3, 7, 1] operations = ["find", "insert", "insert", "find", "find"] values = [3, 4, 9, 2, 4] return = [1, 7, 3] Initially the sorted values are [1,3,7], so the 3rd largest is 1. After inserting 4 and 9, the values are [1,3,4,7,9]; the 2nd largest is 7 and the 4th largest is 3. Example 2 initialValues = [5] operations = ["insert", "find", "insert", "find"] values = [2, 1, 10, 2] return = [5, 5] After inserting 2, the largest value is 5. After inserting 10, the 2nd largest value is still 5. Constraints operations.length == values.length Each operation is either "insert" or "find". For each "find" operation, 1 <= k <= the current list size.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trap is reaching for a size-k min-heap. That only works when k is fixed. Here k varies per query, so you need rank queries on a growing multiset. Two clean options. First, keep a sorted list and use binary search (bisect insort) to insert, then index from the end for the k-th largest. Insertion is O(n) shifting but fast in practice. Second, if values are bounded or you can collect all values offline, compress coordinates and use a Fenwick tree with counts, then find the k-th largest by descending the tree. That gives O(log n) per operation. Common pitfalls: off-by-one when converting k-th largest to index (it's n-k in ascending order), forgetting duplicates, and forgetting the initial values need sorting first. Check Example 2: duplicates and ordering matter. If the Fenwick approach slips your mind mid-OA, StealthCoder can hand you a working version as a hedge.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Dynamic Kth Largest Queries 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 Amazon's OA.
Amazon 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.
Dynamic Kth Largest Queries FAQ
What's the trick in Dynamic Kth Largest Queries?+
k changes every query, so a fixed-size heap fails. Keep values in an ordered structure. A sorted list with binary search insertion works, and a Fenwick tree over compressed values is the faster option. The k-th largest is index n-k in ascending order.
How hard is this Amazon OA question really?+
Medium. The logic is simple once you drop the heap idea. The difficulty is picking a structure that handles inserts and rank lookups efficiently. A sorted list with bisect passes many cases, and a Fenwick tree is the safer pick for large inputs.
Is a heap enough for this problem?+
Not cleanly. A min-heap of size k only answers one fixed k. Since each find supplies a different k and the list keeps growing, you'd have to rebuild or pop repeatedly. Use a sorted structure or a counting tree instead.
How do I handle duplicates in the list?+
Treat each inserted value as its own element. With a sorted list, bisect insertion keeps duplicates in place. With a Fenwick tree, increment the count at that value's compressed index. The k-th largest then counts duplicates toward rank, as in the examples.
How do I prepare for this in 48 hours?+
Write the bisect insort version first and test it on both examples. Then write the Fenwick tree version with coordinate compression and a descent for the k-th element. Practice the n-k index conversion until it's automatic. Two solid implementations beat ten half-read ones.