PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question tests practical coding ability in robotics simulation, specifically implementing unicycle kinematics and discrete-time modeling via forward Euler integration. It evaluates numerical methods, geometric collision detection, and object-oriented design — competencies commonly assessed for robotics and systems software engineering roles.

  • medium
  • Applied
  • Coding & Algorithms
  • Software Engineer

Multi-Agent Collision Simulator (Unicycle Kinematics)

Company: Applied

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

# Multi-Agent Collision Simulator (Unicycle Kinematics) You are given $N$ agents moving in a 2D plane. Each agent is a disk of fixed radius and moves according to a simple **unicycle (differential-drive) motion model** with constant linear speed and constant yaw (turn) rate. Write a simulator that advances all agents in fixed timesteps and prints the **first time any two agents collide**, then stops. If no collision ever occurs within the simulation horizon, report that instead. ## Motion Model Each agent has a state $(x, y, \theta)$ — position in meters and heading in radians. With constant linear speed $v$ (m/s) and constant yaw rate $\omega$ (rad/s), one step of **forward Euler integration** over timestep $\Delta t$ is: $$x \mathrel{+}= v \cdot \cos(\theta) \cdot \Delta t$$ $$y \mathrel{+}= v \cdot \sin(\theta) \cdot \Delta t$$ $$\theta \mathrel{+}= \omega \cdot \Delta t$$ All agents advance one step together, using the same fixed $\Delta t = 0.01$ s. ## Collision Definition Each agent is a circle of fixed radius $r_i$. Two agents $i$ and $j$ **collide** when the Euclidean distance between their centers is less than or equal to the sum of their radii: $$\sqrt{(x_i - x_j)^2 + (y_i - y_j)^2} \le r_i + r_j$$ (Touching counts as a collision.) Agents may already be overlapping at $t = 0$; this must be detected as a collision at time $0.00$. ## Input Each agent is described by six values: | Field | Type | Meaning | |-------|------|---------| | `x` | float | initial x position (meters) | | `y` | float | initial y position (meters) | | `theta` | float | initial heading (radians) | | `v` | float | constant linear speed (m/s) | | `omega` | float | constant yaw rate (rad/s) | | `r` | float | radius (meters) | The input is a list of $N$ agents, each given as the tuple `(x, y, theta, v, omega, r)`. The fixed timestep is $\Delta t = 0.01$ s. Simulate up to a horizon of $T_{\max} = 100.0$ s. ## Output - If a collision occurs, print exactly one line giving the simulated time of the **first** collision, formatted to two decimal places: ``` Collision occurred at 11.53 seconds ``` The simulated clock time at step $k$ is $k \times \Delta t$ (use multiplication, not accumulation, to avoid floating-point drift). A pre-existing overlap at $t = 0$ should be reported as `Collision occurred at 0.00 seconds`. - If no collision occurs by $T_{\max}$, print exactly: ``` No collision ``` When multiple pairs collide on the same step, any single collision time (which is identical for all of them) is correct — you only report the time, not which pair. ## Constraints - $1 \le N \le 500$ - Coordinates, speeds, and yaw rates are finite real numbers; radii are positive. - $\Delta t = 0.01$ s (fixed); $T_{\max} = 100.0$ s, so there are at most $10{,}000$ steps. - Check collisions over all unordered pairs $(i, j)$ with $i < j$ to avoid double-counting; the approach must extend naturally to all $N$ agents. ## Example Two agents on the x-axis moving toward each other: - Agent 0: `(x=0.0, y=0.0, theta=0.0, v=1.0, omega=0.0, r=0.5)` — at the origin, heading $+x$, moving right at 1 m/s. - Agent 1: `(x=9.15, y=0.0, theta=3.14159265, v=1.0, omega=0.0, r=0.5)` — at $x=9.15$, heading $-x$ (pointing toward agent 0), moving left at 1 m/s. The gap between centers starts at $9.15$ m and closes at $2$ m/s. The sum of radii is $r_0 + r_1 = 1.0$ m, so collision first occurs when the gap has closed by $9.15 - 1.0 = 8.15$ m. That takes $8.15 / 2 = 4.075$ s of real time, which falls between step 407 ($t = 4.07$ s, center distance $= 9.15 - 2 \times 4.07 = 1.01 > 1.0$, no collision) and step 408 ($t = 4.08$ s, center distance $= 9.15 - 2 \times 4.08 = 0.99 \le 1.0$, collision). Expected output: ``` Collision occurred at 4.08 seconds ```

Quick Answer: This question tests practical coding ability in robotics simulation, specifically implementing unicycle kinematics and discrete-time modeling via forward Euler integration. It evaluates numerical methods, geometric collision detection, and object-oriented design — competencies commonly assessed for robotics and systems software engineering roles.

You are given `N` agents moving in a 2D plane. Each agent is a disk of fixed radius that moves under a **unicycle (differential-drive) motion model** with constant linear speed `v` and constant yaw rate `omega`. Write a function `simulate(agents)` that advances all agents together in fixed timesteps of `dt = 0.01` s, up to a horizon of `T_max = 100.0` s (at most 10,000 steps), and returns a string reporting the **first time any two agents collide**. ## Motion model (forward Euler) Each agent has state `(x, y, theta)`. One step over `dt`: ``` x += v * cos(theta) * dt y += v * sin(theta) * dt theta += omega * dt ``` All agents advance one step together using the same fixed `dt = 0.01`. ## Collision Each agent `i` is a circle of radius `r_i`. Agents `i` and `j` **collide** when the Euclidean distance between their centers is **≤** the sum of their radii (touching counts): ``` sqrt((x_i - x_j)^2 + (y_i - y_j)^2) <= r_i + r_j ``` Agents may already overlap at `t = 0`; that must be reported as a collision at time `0.00`. ## Input `agents` is a list of `N` tuples, each `(x, y, theta, v, omega, r)`: | Field | Meaning | |-------|---------| | `x`, `y` | initial position (meters) | | `theta` | initial heading (radians) | | `v` | constant linear speed (m/s) | | `omega` | constant yaw rate (rad/s) | | `r` | radius (meters), positive | ## Output (return value) - If a collision occurs, return the first collision time formatted to two decimals: `"Collision occurred at 4.08 seconds"`. The clock time at step `k` is `k * dt` (use multiplication, not accumulation, to avoid float drift). - If no collision occurs by `T_max`, return `"No collision"`. Check all unordered pairs `(i, j)` with `i < j`. When several pairs collide on the same step, the reported time is the same for all of them — you only return the time. ## Constraints - `1 <= N <= 500` - coordinates / speeds / yaw rates are finite reals; radii are positive - `dt = 0.01` s (fixed); `T_max = 100.0` s ⇒ at most 10,000 steps

Constraints

  • 1 <= N <= 500
  • dt = 0.01 s (fixed); T_max = 100.0 s, so at most 10,000 simulation steps
  • Coordinates, speeds, and yaw rates are finite reals; radii are strictly positive
  • Touching counts as a collision (use <=, compare squared distance to (r_i + r_j)^2)
  • Pre-existing overlap at t = 0 must be reported as 'Collision occurred at 0.00 seconds'
  • Clock time at step k is k * dt (multiply, do not accumulate, to avoid float drift)

Examples

Input: ([(0.0, 0.0, 0.0, 1.0, 0.0, 0.5), (9.15, 0.0, 3.14159265, 1.0, 0.0, 0.5)],)

Expected Output: Collision occurred at 4.08 seconds

Explanation: Two agents on the x-axis closing head-on at a combined 2 m/s. Centers start 9.15 m apart; radii sum to 1.0 m, so they touch after the gap closes by 8.15 m. At step 407 (t=4.07) the gap is 1.01 m (no), at step 408 (t=4.08) it is 0.99 m <= 1.0 m (collision).

Input: ([(0.0, 0.0, 0.0, 1.0, 0.0, 0.5), (0.5, 0.0, 0.0, 1.0, 0.0, 0.5)],)

Expected Output: Collision occurred at 0.00 seconds

Explanation: Centers are 0.5 m apart while radii sum to 1.0 m, so the agents already overlap at t=0. The pre-loop / step-0 check must report 0.00 seconds before any integration happens.

Input: ([(0.0, 0.0, 0.0, 1.0, 0.0, 0.5)],)

Expected Output: No collision

Explanation: A single agent has no pairs to check, so no collision can ever occur and the simulator runs to the horizon and returns 'No collision'.

Input: ([(0.0, 0.0, 0.0, 0.0, 0.0, 0.5), (10.0, 0.0, 0.0, 0.0, 0.0, 0.5)],)

Expected Output: No collision

Explanation: Both agents are stationary (v=0, omega=0) and stay 10 m apart, far beyond the 1.0 m touching distance, for the entire 100 s horizon.

Input: ([(0.0, 0.0, 0.0, 1.0, 0.0, 0.5), (3.0, 0.0, 3.14159265, 1.0, 0.0, 0.5)],)

Expected Output: Collision occurred at 1.01 seconds

Explanation: Head-on closing over a 3 m gap at 2 m/s with radii sum 1.0 m. The continuous contact time is 1.00 s, but the discrete Euler check first satisfies distance <= 1.0 m at step 101 (t=1.01) — illustrating that the stepwise simulation, not the analytic formula, decides the reported tick.

Input: ([(0.0, 0.0, 1.5707963267948966, 1.0, 0.0, 0.5), (0.0, 9.15, -1.5707963267948966, 1.0, 0.0, 0.5)],)

Expected Output: Collision occurred at 4.08 seconds

Explanation: Same geometry as the canonical example but rotated onto the y-axis (headings +pi/2 and -pi/2). Verifies that cos/sin integration handles non-axis-aligned headings: collision again at 4.08 s.

Input: ([(-5.0, 0.0, 0.0, 2.0, 0.0, 1.0), (5.0, 0.0, 3.14159265, 2.0, 0.0, 1.0)],)

Expected Output: Collision occurred at 2.01 seconds

Explanation: Faster agents (v=2) with larger radii (sum 2.0 m) closing a 10 m gap at 4 m/s. Analytic contact is 2.00 s; the discrete simulator first registers touching at step 201 (t=2.01).

Hints

  1. Check collisions BEFORE advancing, so step k = 0 catches a pre-existing overlap at t = 0.00. Loop k from 0 to max_steps inclusive: test the current configuration, return k * dt if any pair collides, otherwise advance every agent one Euler step.
  2. Avoid the sqrt: a pair collides when (dx*dx + dy*dy) <= (r_i + r_j)^2. This is exact for the '<=' (touching) rule and faster.
  3. Compute the reported time as k * dt, not by adding 0.01 repeatedly — accumulation drifts and can mis-round the formatted value. Advance all agents together using the SAME pre-step heading (forward Euler), then increment k.
  4. Because the simulation is discrete, the first step at which the touching condition holds can be one 0.01 s tick later than the continuous analytic contact time. Trust the stepwise check rather than a closed-form time.
Last updated: Jun 24, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Merge Overlapping Collinear Segments - Applied (hard)
  • Implement a Fixed-Capacity Deque - Applied (medium)
  • Implement Nested Transactional Key-Value Store - Applied (hard)
  • Merge overlapping 2D line segments - Applied (medium)
  • Group duplicate files by content - Applied (easy)