Blocking Connection Pool Operation Trace
Updated
Problem statement
Simulate the observable behavior of a blocking database connection pool over a finite operation trace.
The pool is initialized with maxSize independent connections numbered 0 through maxSize - 1. All connections begin idle, in a queue ordered by increasing ID. Take idle connections from the front of this queue. Each row operations[i] = [type, value] is processed in input order:
type = 0isGETfor the unique request IDvalue. If an idle connection exists, remove the FIFO head, mark it active, and emit[i, 0, value, connectionId]. Otherwise the call waits in a queue, in request order. It emits nothing until a later valid release gives it a connection.type = 1isRELEASEfor connection IDvalue. Emit[i, 1, value, success], wheresuccessis1only when that connection is currently active. A failed or repeated release changes nothing. After a successful release, transfer the connection directly to the oldest blockedGET, if one exists, and then emit that request's assignment event. Otherwise append the connection to the idle FIFO queue.type = 2isACTIVE_COUNT; ignorevalueand emit[i, 2, activeCount, 0].
Return events in the order they are emitted. A GET still blocked after the final operation never completes and produces no event. Return the complete event list as an int[][].
Interview extensions
- Describe a real thread-safe implementation using a semaphore, a blocking queue, and ownership tracking.
- Explain how to prevent or recover leaked connections with bounded leases, health checks, and observability.
Function
simulateConnectionPool(maxSize: int, operations: int[][]) → int[][]Examples
Example 1
maxSize = 2operations = [[0,101],[0,102],[2,0],[0,103],[1,0],[2,0],[1,0],[1,1],[2,0]]return = [[0,0,101,0],[1,0,102,1],[2,2,2,0],[4,1,0,1],[3,0,103,0],[5,2,2,0],[6,1,0,1],[7,1,1,1],[8,2,0,0]]Requests 101 and 102 receive both connections. Request 103 blocks. Releasing connection 0 emits the release result and then completes request 103 with that same connection.
Example 2
maxSize = 1operations = [[0,7],[1,0],[1,0],[2,0]]return = [[0,0,7,0],[1,1,0,1],[2,1,0,0],[3,2,0,0]]The first release succeeds. The repeated release is rejected and does not change the active count.
Example 3
maxSize = 1operations = [[0,10],[0,20],[0,30],[1,0],[1,0],[2,0]]return = [[0,0,10,0],[3,1,0,1],[1,0,20,0],[4,1,0,1],[2,0,30,0],[5,2,1,0]]Requests 20 and 30 wait in FIFO order. Each successful release transfers connection 0 directly to the oldest waiter.
Example 4
maxSize = 1operations = [[0,1],[0,2],[2,0]]return = [[0,0,1,0],[2,2,1,0]]Request 2 remains blocked when the trace ends, so it has no completion event.
Constraints
1 <= maxSize <= 1000001 <= operations.length <= 200000- Each
operations[i]contains exactly two integers. 0 <= type <= 2- Every
GETrequest ID is positive and unique. - A
RELEASEvalue may be any signed 32-bit integer; it succeeds only for a currently active connection ID. - The number of emitted events fits in memory.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Durable Work Queue OperationsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Keyed Bounded Executor ScheduleMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Matching Engine with Order CancellationIMC Trading · ONSITE INTERVIEWIMC Trading interview questions
- Thread-Safe Task Storage and Worker DispatchNuro · ONSITE INTERVIEWNuro interview questions
- Metric Counter with a Fake ClockSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Per-Client Sliding-Window Rate LimiterNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Concurrent Buffered File LoggerDatabricks · ONSITE INTERVIEWDatabricks interview questions
- Article Vote TrackerRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Dynamic Least-Loaded Job DispatcherCresta · PHONE SCREENCresta interview questions
- Single-Flight Chunk CacheModal · PHONE SCREEN