Tip 1 : Do some projects
Tip 2 : Be good in data structure
Tip 1 : Keep it short
Tip 2 : Don't try to add false things.
There were basically 2 questions asked on graph data structure and were quite challenging.
One problem was easy and the rest two were medium.
A basic understanding of graph traversal would suffice to clear this round.
The total mark allocated was 300 out of which I scored around 270 so I qualified for the next round.



Here, sorted paths mean that the expected output should be in alphabetical order.
Given a square matrix of size 4*4 (i.e. here 'N' = 4):
1 0 0 0
1 1 0 0
1 1 0 0
0 1 1 1
Expected Output:
DDRDRR DRDDRR
i.e. Path-1: DDRDRR and Path-2: DRDDRR
The rat can reach the destination at (3, 3) from (0, 0) by two paths, i.e. DRDDRR and DDRDRR when printed in sorted order, we get DDRDRR DRDDRR.
The idea is to do a depth-first search to find all the cycles which are formed and calculate the length of the largest cycle. We are treating the array as a graph of directed edges. Whenever we get into any of the cells in the cycle, using dfs we will visit all the subsequent cells in the cycle. Out of all the cycles, we will return the cycle of maximum length.
The steps are as follows:
Initialize a boolean array ‘visited’ of size N with 0 at all the indexes initially which denotes whether a particular cell is visited or not.
Initialize an integer ‘res’ to 0, which denotes the largest cycle in the maze.
Define a recursive function ‘dfs’ with arguments ‘arr’, ‘positions’, ‘currentCell’, ‘totalLengthCovered’, array ‘visited’, an integer ‘res’ which denotes the given array ‘arr’, a hash map which is used to store the order in which vertices are visited, the current cell for the recursive call, the total number of cells visited in the current recursive call, array to know whether a particular cell is visited or not, and final answer ‘res’.
If the current cell is already visited in any of the previous recursive calls, then return from the function.
If there is no exit from the current cell, then mark the cell as visited and return from the function.
If the current cell is visited in the same recursive call i.e. it is present in the hash map, mark it as visited, take ‘res’ as a maximum of ‘res’ and ‘totalLengthCovered’ - positions[i].
Insert current cell into the hash map (current cell as the key and total length covered in the current recursion as the value).
Recursively call for the connected cell to the current cell and increment the total length covered.
After the recursive call, mark the current cell visited in the array ‘visited’.
Iterate from i = 0 to N-1:
If the ith cell is not visited:
Declare a hash map ‘positions’ that is used to store the order in which the vertices are visited.
Call the recursive function ‘dfs’ with arguments ‘arr’, ‘positions’, ‘i’, 0, ‘visited’, ‘res’.
If ‘res’ is equal to 0, it means that there is no cycle, return -1.
Otherwise, return ‘res’ as the final answer.



The given graph may have connected components.


Here's your problem of the day
Solving this problem will increase your chance to get selected in this company
What is recursion?