title: How Clear is this Interview Question?
body: <p>We are interviewing for a Senior Java Development Role and all three people that we have asked to complete this question gave us the same incorrect answer. The question was done before the interview so they had a lot of time. Their solutions seemed to sort the input by parentId and then childId instead of creating a tree and from the input and then traversing the tree to find the correct order. Is the question not clear enough?</p>
  
  <p><strong>Question:</strong></p>
  
  <p>The following is a simple skills and presentation test for the Java Developer role which must be completed before the telephone interview.</p>
  
  <p>REQUIRED:</p>
  
  <ol>
  <li><p>JUnit Test</p></li>
  <li><p>Implementation of NodeSorter interface</p></li>
  </ol>
  
  <p>QUESTION:</p>
  
  <p>We have a Java object that looks something like this:</p>
  
  <pre><code>public class Node {
  
      public int id;
      public Integer parentId;
  
      public Node(int id, Integer parentId) {
          this.id = id;
          this.parentId = parentId;
      }
  }
  </code></pre>
  
  <p>For example the following list of Node(s) might be displayed graphically as:</p>
  
  <p>Node (id: 1, parentId: null), Node (id: 2, parentId: 1), Node(id: 3, parentId: 1), Node(id: 4, parentId: 2), Node(id: 5, parentId: 3)</p>
  
  <pre><code>              Node (id: 1)
               /            \
              /              \
             /                \
            /                  \
        Node (id: 2)         Node (id : 3)
           /                    \
          /                      \
         /                        \
        /                          \
    Node (id: 4)                 Node (id : 5)
  </code></pre>
  
  <p>Assumptions:</p>
  
  <ol>
  <li><p>There will be always at least one Node</p></li>
  <li><p>There will be one and only one Node with a null parentId</p></li>
  <li><p>Every Node will have a valid parentId except for the Node which has a null parentId</p></li>
  </ol>
  
  <p>Requirements:</p>
  
  <ol>
  <li>Write a class that implements the following interface that will receive a List of Node(s) and order them from top to bottom (Nodes that are higher in the tree must be before the nodes lower in the tree. Eg. Node 1 on the top of the tree must be before node 4 which is on the bottom of the tree). Nodes on the same level will be in order of their id so the Node with id=2 will appear before the node with id=3 in the diagram above.</li>
  </ol>
  
  <p>Interface:</p>
  
  <pre><code>    public interface NodeSorter {
          public List&lt;Node&gt; sort(List&lt;Node&gt; unSortedNodes);
      }
  </code></pre>
  
  <p>Test Data:</p>
  
  <p>Test Case 1:</p>
  
  <hr>
  
  <p>Diagram of Input:</p>
  
  <pre><code>              Node (id: 1)
               /            \
              /              \
             /                \
            /                  \
        Node (id: 2)   Node (id : 3)
           /                    \
          /                      \
         /                        \
        /                          \
    Node (id: 4)           Node (id : 5)
  </code></pre>
  
  <p>Input: Node (id: 2, parentId: 1), Node(id: 4, parentId: 2), Node (id: 1, parentId: null), Node(id: 3, parentId: 1), Node(id: 5, parentId: 3)</p>
  
  <p>Output: Node (id: 1, parentId: null), Node (id: 2, parentId: 1), Node(id: 3, parentId: 1), Node(id: 4, parentId: 2), Node(id: 5, parentId: 3)</p>
  
  <p>Test Case 2:</p>
  
  <hr>
  
  <p>Diagram of Input:</p>
  
  <pre><code>              Node (id: 1)    
               /            \
              /              \
             /                \
            /                  \
        Node (id: 5)   Node (id : 2)
           /                    \
          /                      \
         /                        \    
        /                          \
    Node (id: 4)           Node (id : 3)
  </code></pre>
  
  <p>Input: Node (id: 5, parentId: 1), Node(id: 4, parentId: 5), Node (id: 1, parentId: null), Node(id: 3, parentId: 2), Node(id: 2, parentId: 1)</p>
  
  <p>Output: Node (id: 1, parentId: null), Node (id: 2, parentId: 1), Node(id: 5, parentId: 1), Node(id: 3, parentId: 2), Node(id: 4, parentId: 5)</p>