Issue and Reuse 16-Bit Tokens
Updated
Problem statement
Implement a token issuer with capacity capacity. Its possible token IDs are the unsigned 16-bit integers 0 through capacity - 1. A token is active after it is issued and before it is returned.
Process operations from left to right. An operation -1 requests a token. If any previously issued token has been returned and is still available, issue the smallest returned token. Otherwise, issue the smallest token that has never been issued, starting from 0. If no token is available, output -1 for that request.
A nonnegative operation t returns token t. Only a currently active token can be returned; a duplicate return, a never-issued token, or a token outside the configured capacity has no effect. Return an array containing one result for each request operation, in request order. Return operations add no result.
Function
issueTokens(capacity: int, operations: int[]) → int[]Examples
Example 1
capacity = 4operations = [-1,-1,-1,1,0,-1,-1,-1]return = [0,1,2,0,1,3]The first three requests issue 0, 1, and 2. After 1 and 0 are returned, the issuer reuses 0 then 1 before issuing fresh token 3.
Example 2
capacity = 2operations = [1,-1,0,0,-1,-1,-1,1,-1]return = [0,0,1,-1,1]The first return of 1 is ignored because 1 was never issued. The repeated return of 0 is ignored. After both tokens are active, the next request is exhausted; returning 1 makes it available again.
Example 3
capacity = 0operations = [-1,-1]return = [-1,-1]Capacity zero has no token to issue.
Constraints
0 <= capacity <= 65536.1 <= operations.length <= 100000.- Each operation is either
-1or an integer from0through65535. - Returning a token that is not active is allowed and has no effect.
More Meta Onsite Interview questions
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
- Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
- Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
- Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
- Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
- Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Assign Partitions to the Least-Loaded ServersAmazon · ONSITE INTERVIEW
- Cursor-Based Pagination Over Sorted LogsRoblox · ONSITE INTERVIEWRoblox interview questions
- Weighted Server Load Balancer with TTLStripe · ONSITE INTERVIEWStripe interview questions
- Meeting Rooms III: Most Booked RoomGoogle · ONSITE INTERVIEWGoogle interview questions
- Insert into a Min-HeapGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Order Matching on ExecuteSquarepoint Capital · ONSITE INTERVIEWSquarepoint Capital interview questions
- Shortest-Job-First Process SchedulingByteDance · PHONE SCREENByteDance interview questions
- Dynamic Batch Completion TimesAmbience Healthcare · PHONE SCREEN
- Automation Pipeline, Part 3: Stage DurationPlaid · PHONE SCREENPlaid interview questions
- Find Total WeightJP Morgan Chase · OAJP Morgan Chase interview questions
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences