Design synchronization for water molecule assembly

Quick Overview

This question evaluates concurrency and synchronization skills, including thread coordination, use of locks/semaphores/condition variables, and reasoning about safety and liveness properties in concurrent systems.

Design synchronization for water molecule assembly

Company: Tesla

Role: Software Engineer

Category: System Design

Difficulty: medium

Interview Round: HR Screen

Multiple threads repeatedly provide 'H' and 'O' atoms as callable actions. Design synchronization so that the program outputs a valid sequence representing water molecules, where for every molecule exactly two 'H' actions and one 'O' action are released together. Do not busy-wait; use semaphores/locks/condition variables. Ensure safety (no extra atoms), liveness (no starvation), and scalability under an unbounded stream of atoms. Provide pseudocode and explain correctness.

Quick Answer: This question evaluates concurrency and synchronization skills, including thread coordination, use of locks/semaphores/condition variables, and reasoning about safety and liveness properties in concurrent systems.

|Home/System Design/Tesla
Tesla logo
Tesla
Aug 14, 2025, 12:00 AM
mediumSoftware EngineerHR ScreenSystem Design
20
0

Concurrency: Build H2O from concurrent H and O actions

Context

You are given two kinds of threads that repeatedly call the following functions:

  • hydrogen(releaseHydrogen)
  • oxygen(releaseOxygen)

Each function receives a callable action:

  • releaseHydrogen() emits/prints "H"
  • releaseOxygen() emits/prints "O"

Goal

Design synchronization so that the program outputs a valid sequence of water molecules. For every molecule, exactly two H actions and one O action must be released together (i.e., the three actions for a molecule proceed as a group; groups do not interleave).

Constraints

  • Do not busy-wait. Use semaphores, locks, and/or condition variables.
  • Safety: no extra atoms are emitted; every molecule releases exactly two H and one O.
  • Liveness: no starvation assuming both atom types arrive infinitely often.
  • Scalability: handle an unbounded stream of atoms without unbounded resource growth.

Deliverables

  • Pseudocode for hydrogen and oxygen functions and any supporting synchronization primitives.
  • A brief explanation of correctness (safety, liveness, scalability).

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...