Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: A robot starts at (0, 0) facing north on an unbounded grid. Instructions: 'G' moves one step forward; 'L' turns 90° left; 'R' turns 90° right. Apply GRGLGLGGRG (10 instructions). What is the robot's distance (Manhattan) from the origin afterwards?
- A. 5
- B. 3
- C. 4
- D. 6
Correct answer: C. 4
Track the heading as an index into the four directions and turn by modular arithmetic. Turning the heading the wrong way around changes the final position.
More Coding Interview Problems questions
- Problem: Elevator starts at floor 0. Requests arrive in order for floors 3, 1, 4 (the elevator serves each request by going directly to…
- Problem: A traffic light cycles green for 30 s, then yellow for 5 s, then red for 25 s, starting at time 0 with green. How many whole…
- Problem: A water-jug problem: a 3-litre jug and a 5-litre jug, both empty, and unlimited water. Allowed actions: fill a jug fully, empty a…
- Problem: The string "PAYPALISHIRING" is written in a zigzag on 3 rows (letters go down the rows in a zigzag, then the rows are read left…
- Problem: How many ordered pairs (i, j) with 1 ≤ i < j ≤ 10 satisfy i XOR j = 3?
- Problem: In how many ways can 100 be written as a sum of two perfect squares x² + y² with 0 ≤ x ≤ y (unordered, non-negative)?