Juspay | OA | Maximum Weight Node
Anonymous User
55888

Hi all,
I recently appeared for Juspay OA for SDE-1. There were three questions to be solved, I was only able to solve this one. I am posting the question along with the test case and my solution.

Problem Description : You are given a maze with N cells. Each cell may have multiple entry points but not more than one exit (i.e. entry/exit points are unidirectional doors like valves).

The cells are named with an integer from 0 to N-1.

You have to find : Find the node number of maximum weight node (Weight of a node is the sum of all nodes pointing to that node).

INPUT FORMAT :

  1. The first line contains the number of cells N.
  2. The second line has a list of N values of the edge[ ] array, where edge[i] conatins the cell number that can be reached from cell 'i' in one step. edge[i] is -1 if the ith doesn't have ans exit.

OUTPUT FORMAT :

  1. First line denotes the node number with maximum weight node.

Sample Input :
23
4 4 1 4 13 8 8 8 0 8 14 9 15 11 -1 10 15 22 22 22 22 22 21

Sample Output :
22

image

Working Solution :

int solution(vector<int>arr){
    int ans=INT_MIN;
    int result=-1;
    vector<int>weight(arr.size(),0);
    for(int i=0;i<arr.size();i++){
        int source=i;
        int dest=arr[i];
        if(dest!=-1){
            weight[dest]+=source;
            if(ans<=weight[dest]){
                ans=max(ans,weight[dest]);
                result=dest;
            }
            
        }
    }
    if(ans!=INT_MIN)
        return result;
    return -1;
}

The other 2 questions can be found here - https://leetcode.com/discuss/interview-question/2032910/Juspay-or-OA-or-Nearest-Meeting-Cell
https://leetcode.com/discuss/interview-question/1186405/largest-sum-cycle (Not My post).

Kindly upvote, if you found this helpful.

Comments (7)