Network Bridge Port Allocator
Updated
Problem statement
A network bridge has n ports, numbered 0 to n - 1. At the start, every port is free. Build a port manager that supports two calls:
get(): pick the free port with the smallest number, mark it as in use, and return its number. If no port is free, return-1.free(port): markportas free again. If that port is already free, nothing changes.
You get the calls in order: operations[i] is the call's name and args[i] holds its number, if any. Return an array with the value returned by every get call, in order. A free call adds nothing to the array.
Function
managePorts(n: int, operations: String[], args: int[][]) → int[]Examples
Example 1
n = 4operations = ["get","get","get","free","free","get","get","get","free","get"]args = [[],[],[],[1],[1],[],[],[],[0],[]]return = [0,1,2,1,3,-1,0]The first three get calls hand out 0, 1 and 2. Port 1 is then freed twice; the second free finds it already free and changes nothing. The next get returns 1, the smallest free port. Then 3 is the only free port, and after that all four are in use, so the next get returns -1. Port 0 is freed, so the last get returns 0.
Example 2
n = 1operations = ["free","get","get","free","get"]args = [[0],[],[],[0],[]]return = [0,-1,0]Freeing port 0 at the start does nothing, because every port starts free. The first get returns 0, and the second returns -1 because the only port is in use. After port 0 is freed, get returns 0 again.
Example 3
n = 6operations = ["get","get","get","get","free","free","get","get","get"]args = [[],[],[],[],[1],[3],[],[],[]]return = [0,1,2,3,1,3,4]The first four calls hand out 0 to 3. Port 1 is freed first and port 3 second, but get always picks the smallest free number, so the next calls return 1, then 3, then 4.
Constraints
1 <= n <= 1000001 <= operations.length <= 10000andargs.length == operations.length- Each
operations[i]is"get"or"free", andargs[i]is[]for"get"and[port]for"free". 0 <= port < n
More Rippling Phone Screen questions
- Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dependency-Aware Task SchedulerPHONE SCREEN · Seen Nov 2026
- Delivery Cost Tracker with Effective-Dated RatesPHONE SCREEN · Seen Nov 2026
- Camel CardsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best and Worst Camel Card CompletionsPHONE SCREEN · Seen Nov 2026
- Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- In-Memory Priority Task SchedulerPHONE SCREEN · Seen Oct 2026
- Limit an Organization Tree's HeightPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Median Across N Sorted ArraysPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Running Medians and PercentilesStackAdapt · PHONE SCREENStackAdapt interview questions
- Task Scheduler with Prerequisites and Execution PathsBrex · PHONE SCREEN
- Streaming Median of Trade PricesXantium · PHONE SCREEN
- Top K Pins with Insert and QueryPinterest · PHONE SCREENPinterest interview questions
- Alert Engine with a Repeat IntervalChronosphere · PHONE SCREEN
- Worst-Performing Ads by CTR in a Campaign TreeUber · PHONE SCREENUber interview questions
- Timer Job Scheduler Wake-upsRedNote · PHONE SCREEN
- Balanced Session ManagerPalantir · PHONE SCREENPalantir interview questions
- Design a Food Rating SystemApple · PHONE SCREENApple interview questions
- Key-Value Store on a Block DeviceLambda · PHONE SCREEN
Interview reports that mention this question
Rippling interview experiences
- Rippling No-Level Onsite Loops: 24 Experiences (Part 5 of 6)
- Rippling No-Level Screens and Loops: 18 Experiences (Part 4 of 6)
- Rippling No-Level Coding Screens: 37 Experiences (Part 3 of 6)
- Rippling No-Level Design, Screens: 30 Experiences (Part 2 of 6)
- Rippling SWE Interviews, No Level: 156 Experiences (Part 1 of 6)