Problem · Graph

Repeated-Roll Teleporter Reachability

Learn this problem
MediumSplunk logoSplunkFULLTIMEPHONE SCREEN

Problem statement

A board contains integer squares from 1 through end. Starting at start, you may roll a die with faces 1 through sides any number of times.

After each roll, discard a move whose initial landing exceeds end. Otherwise, if the landing square is a teleporter source, move once to its destination without chaining. Return whether square end is reachable. A player already on end has reached it.

Function

canReachEnd(start: int, end: int, sides: int, teleporters: int[][]) → boolean

Examples

Example 1

start = 1end = 8sides = 2teleporters = [[2,6]]return = true

Roll 1 to reach 2, teleport to 6, then roll 2 to reach 8.

Example 2

start = 1end = 6sides = 1teleporters = [[2,1]]return = false

Every roll lands on 2 and returns to 1, so 6 is unreachable.

Constraints

  • 1 <= start <= end <= 10^5.
  • 1 <= sides <= 100.
  • Teleporter sources are unique, and every endpoint is in [1, end].

More Splunk problems

drafts saved locally
public boolean canReachEnd(int start, int end, int sides, int[][] teleporters) {
    // Return whether end is reachable.
}
start1
end8
sides2
teleporters[[2,6]]
expectedtrue
checking account