Signed Heading Error and Circular Mean for Waypoint Steering

Last updated: November 29, 2026

Quick Overview

Write a function that returns the signed smallest turn from the current heading to a target heading in degrees, and another that averages a list of compass headings correctly across the 0 and 360 boundary.

Shield AI
Coding & Algorithms
Software Engineer
Shield AI
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Easy

0

0

0 solved


Write a function that returns the signed smallest turn from the current heading to a target heading in degrees, and another that averages a list of compass headings correctly across the 0 and 360 boundary.

Guidance code that steers a V-BAT toward its next waypoint needs a heading error that never commands a 340 degree turn when a 20 degree turn would do. This screens for comfort with angle wraparound and the difference between arithmetic and circular averaging, which shows up throughout autonomy and estimation code.

What the Interviewer Expects
  • Normalizes the difference into a half-open interval and states which end is closed
  • Explains why the arithmetic mean of 350 and 10 is wrong
  • Uses unit-vector summation and atan2 for the circular mean
  • Handles headings that cancel out
  • Guards against floating point results of 360.0
Key Topics to Cover
Angle wraparound
Modular arithmetic
Circular statistics
Guidance
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you weight the circular mean by sensor confidence?
  • Extend the error function to radians in (-pi, pi] without branching.
  • How would you smooth a noisy heading stream over time?
  • Why might a controller want hysteresis near the 180 degree ambiguity?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

heading_error(target, current) returns a value in (-180, 180], positive meaning turn clockwise (right) in compass convention. mean_heading(list) returns a value in [0, 360) or None when undefined....

High-Level Design

For the error, take (target - current) mod 360 to land in [0, 360), then shift anything above 180 down by 360. Exactly 180 stays 180, which picks one direction deterministically.

For the mean, aver...


Submit Your Answer
Markdown supported

Related Questions