Jump Game Microsoft OA 2023

Jump Game Solution

Given an array of non-negative integers arr, you are initially positioned at start index of the array. When you are at index i, you can jump to i + arr[i] or i – arr[i], check if you can reach to any index with value 0. Notice that you can not jump outside of the array at any time.

Example 1:

Input: arr = [4,2,3,0,3,1,2], start = 5
Output: true
Explanation: 
All possible ways to reach at index 3 with value 0 are: 
index 5 -> index 4 -> index 1 -> index 3 
index 5 -> index 6 -> index 4 -> index 1 -> index 3 

Example 2:

Input: arr = [4,2,3,0,3,1,2], start = 0
Output: true 
Explanation: 
One possible way to reach at index 3 with value 0 is: 
index 0 -> index 4 -> index 1 -> index 3

Example 3:

Input: arr = [3,0,2,1,2], start = 2
Output: false
Explanation: There is no way to reach at index 1 with value 0.

Constraints:

  • 1 <= arr.length <= 5 * 104
  • 0 <= arr[i] < arr.length
  • 0 <= start < arr.length

SOLUTION

Program: Jump Game Solution in Python

class Solution:
    def canReach(self, arr: List[int], start: int) -> bool:
        import collections
        queue=collections.deque()
        queue.append(start)
        s=set()
        
        while len(queue):
            curr = queue.popleft()
            
            if arr[curr]==0:
                return True
            
            if curr not in s:
                s.add(curr)
                if curr + arr[curr] < len(arr): 
                    queue.append(curr + arr[curr])
                if curr - arr[curr] > -1: 
                    queue.append(curr - arr[curr])
                    
        return False

Program: Jump Game Solution in Python

class Solution:
    def canReach(self, arr: List[int], start: int) -> bool:
        seen, temp = set(), [start]
        while temp:
            i = temp.pop()
            if arr[i] == 0: return True
            else: seen.add(i)
            if 0 <= i - arr[i] < len(arr) and i - arr[i] not in seen:
                temp.append(i - arr[i])
            if 0 <= i + arr[i] < len(arr) and i + arr[i] not in seen:
                temp.append(i + arr[i])   
        return False

Program: Jump Game Solution in Java

/*
Classic DFS question.
For DFS loops, modify the arr value to go beyound valid as "visited".
*/
class Solution {
    public boolean canReach(int[] arr, int start) {
        if (start < 0 || start >= arr.length || arr[start] >= arr.length) {
            return false;
        }
        if (arr[start] == 0) {
            return true;
        }
        int move = arr[start];
        arr[start] = arr.length;
        return canReach(arr, start - move) || canReach(arr, start + move);
    }
}

Program: Jump Game Solution in C++

class Solution {
public:
    int solve(vector<int>& arr, int idx){
        if(idx<0 || idx>arr.size()-1 || arr[idx]==-100) return 0;
        if(arr[idx]==0) return 1;
        int temp = arr[idx]; 
        arr[idx]=-100;
        bool right = solve(arr,idx+temp);
        bool left = solve(arr,idx-temp);
        arr[idx]=temp;
        return (right || left) ? 1 : 0;
    }
    
    bool canReach(vector<int>& arr, int start) {
        return solve(arr, start)==1 ? true : false;
    }
};

Microsoft Online Assessment 2023 Questions List

Leave a Comment

5 − three =