Coordinate Counter Updates Across Threads

Quick Overview

Coordinate two threads that increment a counter from zero: one snapshots at 10, the other snapshots one second after a recorded synchronized launch, then combine both safely.

Coordinate Counter Updates Across Threads

Company: Tesla

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Onsite

# Coordinate Counter Updates Across Threads Design a thread-safe program in which two workers modify one shared counter. One worker stops when the counter reaches 10 and returns a snapshot. The other stops after one second and returns a snapshot. The coordinator waits for both and prints the sum of their snapshots. ### Constraints & Assumptions - The source does not prescribe the update rule. For this illustrative implementation, the counter starts at `0` and both workers increment it by `1` per update. - Both workers first wait at a start gate. After they are ready, the coordinator records a monotonic launch instant and immediately opens the gate; the timed worker's deadline is exactly one second after that recorded instant. - Use a monotonic clock for the elapsed-time condition. - The exact snapshots may vary with scheduling; correctness concerns synchronization and termination, not a fixed numeric output. ### Clarifying Questions to Ask - Would a production version use the illustrative initial value and unit-increment rule, and may the counter overshoot 10? - Is the agreed deadline origin the coordinator's recorded gate-release instant, or does the interviewer want another event to start the timer? - Must the first worker terminate if the timed worker stops before the counter reaches 10? ```hint Define shared and private state The counter, stop conditions, snapshots, and completion signals do not all require the same synchronization strategy. ``` ### What a Strong Answer Covers - A race-free counter update and snapshot operation. - Precisely defined termination and no deadlock or orphan thread. - Monotonic time, bounded waiting, and exception propagation. - Recognition that scheduling makes the numerical result nondeterministic. ### Follow-up Questions - How would you test this without waiting one real second? - When would an atomic counter be insufficient?

Overview: Coordinate two threads that increment a counter from zero: one snapshots at 10, the other snapshots one second after a recorded synchronized launch, then combine both safely.

|Home/Software Engineering Fundamentals/Tesla
Tesla logo
Tesla
Aug 24, 2026
mediumSoftware EngineerOnsiteSoftware Engineering Fundamentals
2
0

Coordinate Counter Updates Across Threads

Design a thread-safe program in which two workers modify one shared counter. One worker stops when the counter reaches 10 and returns a snapshot. The other stops after one second and returns a snapshot. The coordinator waits for both and prints the sum of their snapshots.

Constraints & Assumptions

  • The source does not prescribe the update rule. For this illustrative implementation, the counter starts at 0 and both workers increment it by 1 per update.
  • Both workers first wait at a start gate. After they are ready, the coordinator records a monotonic launch instant and immediately opens the gate; the timed worker's deadline is exactly one second after that recorded instant.
  • Use a monotonic clock for the elapsed-time condition.
  • The exact snapshots may vary with scheduling; correctness concerns synchronization and termination, not a fixed numeric output.

Clarifying Questions to Ask Guidance

  • Would a production version use the illustrative initial value and unit-increment rule, and may the counter overshoot 10?
  • Is the agreed deadline origin the coordinator's recorded gate-release instant, or does the interviewer want another event to start the timer?
  • Must the first worker terminate if the timed worker stops before the counter reaches 10?

What a Strong Answer Covers Guidance

  • A race-free counter update and snapshot operation.
  • Precisely defined termination and no deadlock or orphan thread.
  • Monotonic time, bounded waiting, and exception propagation.
  • Recognition that scheduling makes the numerical result nondeterministic.

Follow-up Questions Guidance

  • How would you test this without waiting one real second?
  • When would an atomic counter be insufficient?
Loading comments...