Google Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
Not sure about the question. But i will answer some of the specific points in the question.
How to differentiate between different nodes with same data and encountering the same node? A: use the address of the node to differentiate
also how do you make sure you don't run into loops for the exact node? A: as its done in DFS.
BFS - for accessing all vertex and edges and handling cycles with love.
for every vertex you take out from queue, clone it. store in hashmap <orinigal,clone>.
if adjacent is found for first time,
clone vertex store in hashmap <orinigal,clone> and add edge in clone adjacency.
else
take out the visited node for hashmap, add edge, put it back.
vertex and edges need to be objects. you can have 2 vertex with same value.
stackoverflow . com/questions/12886036/deep-copying-a-graph-structure
- hirajhil January 26, 2014