networking · intermediate · ~15 min

Peak connections

Track a high-water mark.

Challenge

Using the same connect/disconnect log, report the high-water mark — the maximum number of clients connected simultaneously at any point. Knowing peak concurrency sizes your connection table and worker pool.

Task

Implement int peak_connections(const char *ops) that returns the maximum running connection count reached while processing ops ('c' = connect, 'd' = disconnect, with the count floored at 0).

Input

A string ops of 'c' and 'd' characters.

Output

Returns the peak simultaneous connection count.

Example

peak_connections("cccddc")   ->   3
peak_connections("cdcd")     ->   1

Edge cases

  • Track the maximum as the count rises; disconnects do not lower the recorded peak.

Input format

A string ops of 'c' (connect) and 'd' (disconnect) characters.

Output format

The maximum simultaneous connection count reached.

Constraints

Maintain a running count (floored at 0) and remember its maximum.

Starter code

int peak_connections(const char *ops) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.