Compute the Nth Recency Value
Company: Bloomberg
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This question evaluates understanding of recurrence-based sequence generation, index-based historical state tracking, and the design of time- and space-efficient algorithms. It is commonly asked in the Coding & Algorithms domain to assess algorithmic analysis and performance reasoning, emphasizing practical implementation-level competency rather than purely theoretical concepts.
Constraints
- 1 <= n <= 1000000
- An O(n^2) simulation that scans backward every step will be too slow for large n.
Examples
Input: 1
Expected Output: 0
Explanation: The sequence starts with a[0] = 0, so the first value is 0.
Input: 2
Expected Output: 0
Explanation: a[1] = 0 because the previous value 0 had not appeared before index 0.
Hints
- You only need to remember the most recent previous index of each value, not every occurrence.
- While generating the sequence, update the last seen position of the previous value before moving to the next value.