Van Eck Sequence Term
Updated
Problem statement
Given a positive integer n, define a zero-indexed sequence a as follows:
a[0] = 0.- For every
i >= 1, letx = a[i - 1]. Ifxappeared ina[0..i-2], letjbe its greatest previous index and seta[i] = (i - 1) - j. Otherwise, seta[i] = 0.
Return a[n - 1].
Function
vanEckTerm(n: int) → intExamples
Example 1
n = 1return = 0The requested term is the initial value a[0].
Example 2
n = 4return = 0The first four terms are [0, 0, 1, 0], so a[3] = 0.
Example 3
n = 10return = 6The prefix is [0, 0, 1, 0, 2, 0, 2, 2, 1, 6]. Before index 9, the previous value 1 last appeared at index 2, so a[9] = 8 - 2 = 6.
Constraints
1 <= n <= 1000000.
More Bloomberg LP Onsite Interview questions
- Pascal's TrianglePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Valid Triangle NumberPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Transactional Key-Value StoreGrammarly · ONSITE INTERVIEW
- Invoice / Payment ReconciliationStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Sustained Overspeed AlertsFleetx · ONSITE INTERVIEW
- Route Pattern MatcherGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions