Fixed-Size Connection Pool
Design a fixed-size pool of reusable connections: hand out the lowest free id on request, take ids back on release, and always know how many are free.
Requirements
- The pool owns exactly
sizeconnections with ids0 .. size-1; all start free. acquirealways returns the lowest-numbered free id (and marks it busy), or-1when the pool is fully checked out. Determinism matters — the same state must always hand out the same id.release(id)returns a connection to the pool. It must be idempotent and defensive: releasing an id that is out of range, or one that is already free, is a no-op (never throw, never push the free count abovesize).availablereports the current count of free connections.- Model this single-threaded; there is no real blocking or waiting.
API to implement
Implement the class ConnectionPool. The I/O driver is already written in the starter code — it parses the command script below and calls your methods. You only implement the class.
| Command | Returns | Behavior |
|---|---|---|
| acquire | int | Mark the smallest-index free connection busy and return its id, or return -1 if every connection is currently busy. |
| release <id> | — | Mark connection id free so it can be handed out again. Silently ignore the call if id is out of range (< 0 or ≥ size) or is already free. |
| available | int | Return how many connections are currently free. |
Input format
The first line contains Q, the number of operations. The second line contains the constructor argument: size — the number of connections in the pool, with ids 0 .. size-1 (all free initially). Each of the next Q lines is one command as listed above. String arguments contain no spaces.
Output format
For each command with a non-void return, print its result on its own line, in order. The driver does this for you — your methods just return values.
This is a design problem: the hidden tests exercise edge cases and interaction sequences, not performance tricks. Model the state cleanly and the tests will pass.
Sample cases
7
2
acquire
acquire
acquire
available
release 0
available
acquire0
1
-1
0
1
08
3
acquire
acquire
release 5
release 2
available
release 0
available
acquire0
1
1
2
0