AlgoFrames

Spot the bug: mutable key

Standard · Find the bug · about 8 min

A robot starts at (0, 0) and follows moves, a string of U, D, L and R steps. This function should return true when the robot stands on some point twice, counting the start. It is wrong for some inputs. Click the line with the bug, then fix it.

Example 1

Input:

["U"]

Output:

false

The robot visits (0,0), then (0,1). The JavaScript code returns true here.

Example 2

Input:

["URDL"]

Output:

true

The fourth step brings the robot back to (0,0).

Constraints

  • 0 ≤ moves.length ≤ 10,000
  • moves contains only U, D, L and R

Learn the idea first: Collisions, worst cases and mutable keys, a visual lesson.

More Collisions, worst cases and mutable keys problems