Nodes Connecting to and from Multiple Other Nodes (in JavaScript or Ruby preferably)

34 views Asked by At

I'm a newbie to data structures + algorithms, and while practicing interview questions, I came across the following: say that we have individual nodes connecting to and from other (possibly multiple) nodes:

(1) Generate the data structure to store it all

(2) Generate the function to retrieve a list of all the nodes within "n" hops from a particular node

I'm particularly not sure about what "data structure" to use. I thought okay, maybe we can create a node class, and have an array to reference all the nodes that a particular node points to. Then, in order to find the nodes that are within "n" hops from a particular node, we can iterate through that array of nodes, recursively call the function (passing n-1 this time) until we hit a base case of "1" hops. Could someone explain the error in my thinking and/or improve on it?

Appreciate any suggestions!

0

There are 0 answers