linux-sysprog · beginner · ~15 min

Has the deadline passed?

Detect a timeout from timestamps.

Challenge

Comparing elapsed time against a budget is the manual version of an alarm timeout. Decide whether a deadline has passed.

Task

Implement int timed_out(long start, long now, long timeout) that returns 1 if now - start >= timeout, else 0.

Input

  • start: start timestamp.
  • now: current timestamp.
  • timeout: allowed duration.

Output

1 if the elapsed time now - start has reached timeout, else 0.

Example

timed_out(100, 160, 50)   ->   1
timed_out(100, 120, 50)   ->   0
timed_out(100, 150, 50)   ->   1   (exactly at the deadline)

Edge cases

  • Elapsed exactly equal to timeout: counts as timed out (return 1).

Input format

start, now: timestamps; timeout: allowed duration.

Output format

1 if now - start >= timeout, else 0.

Constraints

Reaching the deadline exactly counts as timed out.

Starter code

int timed_out(long start, long now, long timeout) {
    /* TODO */
    return 0;
}

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