Fixed-Capacity Circular Buffer
Updated
Problem statement
Implement a circular buffer that holds at most capacity integers. Store the values in one array of length capacity that you create once and never resize. Do not use a built-in queue or deque.
The buffer starts empty. Process operations in order. Each operation is a pair of integers and records exactly one value:
[0, value]: writevalueafter the newest stored value. If the buffer is full, the write is rejected and nothing changes. Record1if the value was stored, or0if it was rejected.[1, 0]: remove the oldest stored value. Record1if a value was removed, or0if the buffer was empty.[2, 0]: record the oldest stored value, or-1if the buffer is empty.[3, 0]: record the newest stored value, or-1if the buffer is empty.[4, 0]: record1if the buffer is empty, otherwise0.[5, 0]: record1if the buffer is full, otherwise0.
Return the recorded values in operation order.
Function
circularBuffer(capacity: int, operations: int[][]) → int[]Examples
Example 1
capacity = 3operations = [[0,10],[0,20],[0,30],[0,40],[3,0],[5,0],[1,0],[0,40],[3,0],[2,0]]return = [1,1,1,0,30,1,1,1,40,20]The first three writes fill the buffer, so the write of 40 is rejected and records 0. The newest value is 30 and the buffer is full. Removing the oldest value drops 10. Now 40 fits. The newest value is 40 and the oldest is 20.
Example 2
capacity = 1operations = [[4,0],[1,0],[2,0],[0,7],[4,0],[5,0],[0,8],[1,0],[3,0]]return = [1,0,-1,1,0,1,0,1,-1]The buffer starts empty, so the removal records 0 and the oldest value is -1. After 7 is written, the buffer of capacity 1 is not empty and is full. The write of 8 is rejected. Removing 7 empties the buffer, so the newest value is -1.
Constraints
1 <= capacity <= 50001 <= operations.length <= 5000operations[i].length == 20 <= value <= 10^6- The first integer of each operation is between 0 and 5. For operations 1 to 5, the second integer is 0.
More Citadel Onsite Interview questions
- Maximize Workday EarningsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Minimum Changes for Palindromic BlocksOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Removal PoolONSITE INTERVIEW · Seen Sep 2026
- Centered K-by-K Submatrix SumsONSITE INTERVIEW · Seen Aug 2026
- Search Suggestions with a Prefix TreeONSITE INTERVIEW · Seen Aug 2026
- Traverse Interior Matrix Cells in Row-Major OrderONSITE INTERVIEW · Seen Aug 2026
- Limit Order Book Matching EnginePHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Stock Trading with a Fee and a CooldownONSITE INTERVIEW · Seen May 2026
- Time-Based Key-Value StorePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2026
- 2048 Simulation and Board CodecONSITE INTERVIEW · Seen Jan 2026
Similar questions at other companies
- Round-Robin Iterator over IteratorsBridge · ONSITE INTERVIEWBridge interview questions
- Design Hit CounterNutanix · ONSITE INTERVIEWNutanix interview questions
- Five-Minute Hit CounterGoogle · ONSITE INTERVIEWGoogle interview questions
- Ring Buffer OperationsAmazon · ONSITE INTERVIEW
- Recent Hit CounterAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Design Hit CounterCharta Health · PHONE SCREENCharta Health interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Implement a FIFO QueueByteDance · PHONE SCREENByteDance interview questions
- Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
- Music Player Operation SequenceAtlassian · PHONE SCREENAtlassian interview questions
Citadel interview experiences
- Citadel Securities Quant Researcher Intern Loop: 1 Experience
- Citadel Securities Research Engineer Interview: 1 Experience
- Citadel Securities SWE Intern First Round: 2 Experiences
- Citadel Securities Experienced SWE Interviews: 1 Experience
- Citadel SWE Intern Later Rounds and Superday: 13 Experiences
- Citadel SWE Intern First-Round Questions: 20 Experiences