Detect When a Delivery Robot Revisits a Sidewalk Intersection

Last updated: November 29, 2026

Quick Overview

A sidewalk delivery robot moves on a city grid following a string of moves N, S, E, W, one block each. Return the index of the first move after which the robot stands on an intersection it has already visited, or -1 if it never does.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


A sidewalk delivery robot moves on a city grid following a string of moves N, S, E, W, one block each. Return the index of the first move after which the robot stands on an intersection it has already visited, or -1 if it never does.

A quick Google screen question that checks you can simulate movement while maintaining visited state. Interviewers look for a set of coordinates, correct handling of the start cell, and returning the precise index rather than just a boolean.

What the Interviewer Expects
  • Include the starting intersection in the visited set before moving
  • Map each move character to a delta in a dictionary
  • Return the zero-based index of the offending move
  • Explain O(n) time and space
  • Validate or reject unknown characters
Key Topics to Cover
simulation
hash set
coordinates
string processing
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 report the length of the loop that was just closed?
  • What if the robot can be told to teleport to a coordinate?
  • How would you handle a stream of moves that never ends?
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: a string path over the alphabet NSEW. Start at (0, 0). Output: the smallest index i such that after executing path[i] the robot is at a point visited earlier (the start counts), else `-...

High-Level Design

Keep a dictionary from character to (dx, dy) and a set seen seeded with the origin. For each move, update the position, check membership, return the index on a hit, otherwise add the position. A b...


Submit Your Answer
Markdown supported