Implement a Vector
Updated
Problem statement
Implement a vector, a dynamic array that keeps its values in one fixed-size array and moves them to a bigger array when it runs out of room. Process operations on it and return the recorded values in operation order.
The vector has a size, the number of values it holds, and a capacity, the length of the array behind it. Both start at 0. When push_back finds the size equal to the capacity, the vector first makes a new array of capacity 1 if the capacity is 0, or twice the capacity otherwise, and copies its values into it. The capacity never shrinks. Do not use a built-in list or array that grows on its own.
Indexes start at 0. Each operation is three integers:
[0, x, 0]:push_back(x)addsxat the end. Nothing is recorded.[1, 0, 0]:pop_back()removes the last value and records it, or records-1if the vector is empty.[2, i, 0]:get(i)records the value at indexi, or-1ifiis not less than the size.[3, i, x]:set(i, x)replaces the value at indexiwithxand records1. Ifiis not less than the size, nothing changes and it records0.[4, 0, 0]:size()records the size.[5, 0, 0]:capacity()records the capacity.
push_back should take amortized O(1) time, and every other operation O(1) time.
Note: amortized O(1) means that any m operations take O(m) time in total, even if a single operation is sometimes slower.
Function
vectorOperations(operations: int[][]) → int[]Examples
Example 1
operations = [[5,0,0],[0,10,0],[0,20,0],[0,30,0],[4,0,0],[5,0,0],[2,1,0],[3,1,25],[2,1,0],[1,0,0],[4,0,0],[5,0,0]]return = [0,3,4,20,1,25,30,2,4]The capacity starts at 0 and becomes 1, 2 and then 4 as 10, 20 and 30 are pushed. set(1, 25) replaces 20, pop_back() returns 30, and the capacity stays 4.
Example 2
operations = [[1,0,0],[2,0,0],[3,0,7],[0,7,0],[2,1,0],[3,1,9],[0,8,0],[1,0,0],[1,0,0],[1,0,0],[4,0,0],[5,0,0]]return = [-1,-1,0,-1,0,8,7,-1,0,2]The first three calls fail on an empty vector and record -1, -1 and 0. Index 1 is out of range while only 7 is stored, and after both values are popped the size is 0 but the capacity stays 2.
Constraints
1 <= operations.length <= 2 * 10^5operations[k].length == 30 <= x, i <= 10^9- Each operation is one of
[0, x, 0],[1, 0, 0],[2, i, 0],[3, i, x],[4, 0, 0]or[5, 0, 0].
More Citadel Phone Screen questions
- Merge Price-Delta FeedsPHONE SCREEN · Seen Dec 2026
- Speed Up a Slow FunctionOA · PHONE SCREEN · Seen Nov 2026
- Portfolio Tickers with Constant-Time Random PickPHONE SCREEN · Seen Nov 2026
- Square Root FunctionPHONE SCREEN · Seen Oct 2026
- Reachable Transfer PositionsPHONE SCREEN · Seen Oct 2026
- Spreadsheet Formula DependenciesPHONE SCREEN · Seen Oct 2026
- Currency Arbitrage CyclePHONE SCREEN · Seen Sep 2026
- Order Book with Cancel by Order IDPHONE SCREEN · Seen Sep 2026
- Smart Queue of Latest PricesPHONE SCREEN · Seen Sep 2026
- Reconcile Unmatched Trades with Timestamp TolerancePHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Logger Rate LimiterGoogle · PHONE SCREENGoogle interview questions
- Lazy Filter over a GeneratorSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Track New Values in Sorted OrderBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Array-Backed Set Without Library CollectionsOde with Anthropic · PHONE SCREEN
- Stepped IteratorBridge · PHONE SCREENBridge interview questions
- Implement an Iterator Next MethodNVIDIA · PHONE SCREENNVIDIA interview questions
- Customer Bootstrap APIDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Design Hit CounterCharta Health · PHONE SCREENCharta Health interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Billing Log with Undo and RedoReddit · PHONE SCREENReddit 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