Track Langton's Ant on an Unbounded Sparse Pixel Canvas

Last updated: November 29, 2026

Quick Overview

An ant sits on an infinite white canvas at (0, 0) facing up. Each step, on a white cell it turns right, on a black cell it turns left; it then flips the cell's color and moves forward one cell. After n steps, return the ant's position, its heading, and the number of black cells.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


An ant sits on an infinite white canvas at (0, 0) facing up. Each step, on a white cell it turns right, on a black cell it turns left; it then flips the cell's color and moves forward one cell. After n steps, return the ant's position, its heading, and the number of black cells.

A Google technical screen simulation where the board is unbounded, so the interviewer checks the choice of a sparse representation over a fixed array. It also probes whether you apply turn, flip and move in the specified order.

What the Interviewer Expects
  • Represent the canvas as a set of black cells rather than a 2D array
  • Apply the rule order exactly: turn, flip, move
  • Encode heading as an index into a direction array
  • Report the black count in O(1) from the set size
  • Discuss memory growth as steps increase
Key Topics to Cover
simulation
sparse grid
hash set
cellular automaton
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 render a viewport of the canvas around the ant?
  • Generalize to a multi-color rule string like RLR.
  • How would you detect the famous highway phase automatically?
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

Input: integer n >= 0. Coordinates are (x, y) with up as +y. Rule per step: if the current cell is white turn 90 degrees right, if black turn left; flip the cell; move one cell forward. Output `(p...

High-Level Design

Use black = set() and heading index d into [(0,1),(1,0),(0,-1),(-1,0)], ordered clockwise so right is +1 and left is +3 mod 4. Each step checks membership, turns, toggles membership (add or ...


Submit Your Answer
Markdown supported