Loading…
Loading…
Given a list of values, spawn one thread per value; each thread proposes its value as the new running maximum. Print the final maximum.
Why this is gradable. Concurrent code can legitimately produce different interleavings on every run, so this practice bank only includes problems whose correctness reduces to one deterministic final value or invariant — never a specific thread ordering. If your solution is correct, it will print the exact same output on every run, no matter how the scheduler interleaves your threads. The max of a fixed set of values is the same no matter what order it's computed in, which is exactly why this is a good first "read shared state, maybe write shared state" synchronization exercise.
Input: 3 7 2 9 4 1 8 5 6 0 15 12
Output: 15
"Check if my value is bigger, then update" is a classic check-then-act race: two threads can both read the old max, both decide they're bigger, and one update can clobber the other — or a thread can read a stale max mid-update. The bug doesn't show up with small lists; it shows up under load, which is exactly why interviewers ask this.
Guard both the read and the write with the same lock, so "check, then act" happens as one atomic step.
Line 1: space-separated integers (one per thread)
Print the final maximum.
Input (stdin)
Output
Input (stdin)
Output
Sign in to track solved problems and earn XP.