FastPrepTop K Recent Unique Values

Top K Recent Unique Values

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

Process the integers in accesses from oldest to newest while maintaining one occurrence of each distinct value in recency order.

When a value is accessed, remove its older position if it is already present, then make it the most recent value. Return up to the first k distinct values from most recent to least recent after every access has been processed.

Function

topKRecentUniqueValues(accesses: int[], k: int) → int[]

Examples

Example 1

accesses = [1,2,3,2,4]k = 3return = [4,2,3]

Reaccessing 2 moves it ahead of 3 and 1; the final access to 4 makes it most recent.

Example 2

accesses = [5,5,5]k = 5return = [5]

The repeated value appears only once, and fewer than five distinct values exist.

Example 3

accesses = []k = 3return = []

No values were accessed, so the recency list is empty.

Constraints

  • 0 <= accesses.length <= 100000
  • -1000000000 <= accesses[i] <= 1000000000
  • 0 <= k <= 100000
  • If fewer than k distinct values exist, return all of them.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int[] topKRecentUniqueValues(int[] accesses, int k) {
    // Write your code here.
}
accesses[1,2,3,2,4]
k3
expected[4,2,3]
Checking account…