Microsoft OA Jump Game Solution

Microsoft OA 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.

Also See: Microsoft Online Assessment Questions and Solution

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 Python: Jump Game in Python BFS Solution, Time Complexity: O(n)

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
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 Java: Jump Game in Java DFS Solution

/*
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 C++: Jump Game in C++ DFS Solution

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;
    }
};

Also See: AMCAT Study Materials, Preparation Guide

Also See: Amazon OA Online Assessment 2021 Questions and Answers

Microsoft Online Assessment 2021 Questions

Leave a Comment