FastPrepFinal Position from Direction Commands

Final Position from Direction Commands

Goldman Sachs logoGoldman Sachs● EasyFULLTIMEINTERNPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Start at coordinate (0, 0) and process commands from left to right. The letters L, R, U, and D move one unit left, right, up, and down. Lowercase forms have the same meaning. Ignore every other character.

Return [x, y] after all commands.

Function

finalCoordinates(commands: String) → int[]

Examples

Example 1

commands = "LRUDx"return = [0,0]

The four moves cancel and x is ignored.

Example 2

commands = "RRuLdd?"return = [1,-1]

Commands are case-insensitive and ? is ignored.

Constraints

  • 0 <= commands.length <= 1000000
  • commands contains ASCII characters.

More Goldman Sachs problems

See Goldman Sachs hiring insights
public int[] finalCoordinates(String commands) {
  // write your code here
}
commands"LRUDx"
expected[0,0]
Checking account…