Amazon Interview Question
Country: United States
You can extended the hashmap idea. Preprocess input to create a HashMap<id(manager), ArrayList<id(sub-ordinates>>.
To print all subordintes of manager you will to first go through all sub-ordinates of manager ie the arraylist, mark them visited and then make them managers and go through their subordinates. Its similar to BFS in a graph.
Why do you have to go for a trie?
why not create a n-tree having nodes like
The root node will have no Manager;
- liju.leelives July 08, 2015If needed you can also maintain a HashTable and an array of linkedlist with <Employee, index>
and LinkedList<Integer>[] subordinates= new LinkedList[];
and index will map to the respective employees subordinate list
i guess all of the operations can be done from these!