I attained an interview where I was asked a question as below:
You are given with
parent -----> child
relationships i.e.N1 ---> N2
whereN1
is the parent ofN2
. This is nothing but representing a binary tree in adjacency list form. So I had to find whether there is a loop is present or not.
I have been mulling over this and came up with a solution:
Just need to check individual node i.e. N1
and try going deep if you see there is a edge coming back to N1
then print. Else go for next node. But the interviewer told me that it is not very efficient, can somebody help me in finding an efficient solution. Thanks.