FastPrepAlternating Direction Jump Game

Alternating Direction Jump Game

Google logoGoogle● HardFULLTIMEPHONE SCREENONSITE INTERVIEW

Problem statement

You are given an integer array values, a starting index start, and a positive integer increment. The first move is odd.

  • On an odd move, scan left from the current index and jump to the nearest index whose value is exactly the current value plus 1.
  • On an even move, scan right and use the same target-value rule.

After a successful jump, add increment to the value at the position you just left, then alternate the move parity. If no valid jump exists, return the current index. If play continues forever, return -1.

Function

playJumpGame(values: int[], start: int, increment: int) → int

Examples

Example 1

values = [3,4,2,2,7]start = 2increment = 4return = 1

The odd move reaches index 0, the even move reaches index 1, and no value 5 exists to its left.

Example 2

values = [2,1]start = 1increment = 2return = -1

The two positions alternate forever while their values rise together.

Constraints

  • 1 <= values.length <= 12.
  • -100 <= values[i] <= 100.
  • 0 <= start < values.length.
  • 1 <= increment <= 20.

More Google problems

See Google hiring insights
Saved
public int playJumpGame(int[] values, int start, int increment) {
    // Write your code here.
}
Checking account…