networking · beginner · ~15 min
Track concurrent connection count.
Replay a log of connect/disconnect events and report how many clients are still connected at the end. Each character in the log is 'c' (a client connected) or 'd' (a client disconnected).
Implement int active_connections(const char *ops) that returns the number of clients connected after processing ops left to right. A 'd' when nobody is connected is ignored (the count never goes negative).
A string ops of 'c' and 'd' characters.
Returns the active connection count at the end.
active_connections("ccdc") -> 2
active_connections("cd") -> 0
active_connections("dd") -> 0 (disconnects with no one connected are ignored)
'd' with a zero count does not decrement below 0.A string ops of 'c' (connect) and 'd' (disconnect) characters.
The number of clients still connected at the end.
Increment on 'c'; decrement on 'd' only when the count is > 0.
int active_connections(const char *ops) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.