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?
Question:
The following is a simple skills and presentation test for the Java Developer role which must be completed before the telephone interview.
REQUIRED:
JUnit Test
Implementation of NodeSorter interface
QUESTION:
We have a Java object that looks something like this:
public class Node {
public int id;
public Integer parentId;
public Node(int id, Integer parentId) {
this.id = id;
this.parentId = parentId;
}
}
For example the following list of Node(s) might be displayed graphically as:
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)
Node (id: 1)
/ \
/ \
/ \
/ \
Node (id: 2) Node (id : 3)
/ \
/ \
/ \
/ \
Node (id: 4) Node (id : 5)
Assumptions:
There will be always at least one Node
There will be one and only one Node with a null parentId
Every Node will have a valid parentId except for the Node which has a null parentId
Requirements:
Interface:
public interface NodeSorter {
public List<Node> sort(List<Node> unSortedNodes);
}
Test Data:
Test Case 1:
Diagram of Input:
Node (id: 1)
/ \
/ \
/ \
/ \
Node (id: 2) Node (id : 3)
/ \
/ \
/ \
/ \
Node (id: 4) Node (id : 5)
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)
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)
Test Case 2:
Diagram of Input:
Node (id: 1)
/ \
/ \
/ \
/ \
Node (id: 5) Node (id : 2)
/ \
/ \
/ \
/ \
Node (id: 4) Node (id : 3)
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)
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)
"I am grateful for interviewing with you today. You have given me a clear overview of the position. I think my experience and accomplishments can provide value to the organization. Is there anything else you need to confirm if I am the right candidate for this position?"
Closed interview questions limit the respondent's options. The examples were selected from different interviews and are not shown in any particular order. A closed question limits the response available to the interviewee. You may be familiar with closed questions through multiple-choice exams in college.
Explain the next step in the process, including whether there will be further interviews, when you will make your decision, and how the candidate will be informed of your decision. Thank the candidate for interviewing for the position. Complete your notes and/or rating sheets immediately; don't rely on your memory.
I would say that this is pretty darn unclear. You want an unsorted list for input, and a sorted list as output.
Each item in the output list is a tree-node, but there is no balance enforced on the tree. Couldn't the user just read in the values for all the nodes, sort them, and then loop over the sorted values creating new nodes, each of which point at the previous node, and essentially be done...they've created a grossly unbalanced tree (no branches), but who cares?
That seems correct to me, but if I was interviewing I would not be very happy with this answer, because I would be wondering why the tree was there at all.
Nothing in this question indicates to me that I should do anything like what you're suggesting: "creating a tree and from the input and then traversing the tree to find the correct order."
Honestly, the question seems pretty clear to me.
Have you tried asking them why they chose to sort by list ID and then node ID? Did they think that solved the problem? If so, what is their reaction when confronted with input for which it doesn't work?
Even if they didn't answer the question correctly, by asking them these questions you can both learn more about them and understand what in your question isn't clear enough.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With