Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link
edited tags
Link
200_success
  • 145.5k
  • 22
  • 190
  • 479
added 237 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Java Interview - Missing level of technical depth (Flatten Tree)

They refused me by saying that"as we felt they didn't demonstrate the level of technical depth we're seeking from candidates"

as we felt they didn't demonstrate the level of technical depth we're seeking from candidates

Question 1 : Java Interview - MissingMissing level of technical depth (Recursive to Iterative)

Question 3 : Java Interview - MissingMissing level of technical depth (Common Ancestor)

Question 2: Implement a method that, given a tree as a parameter, will return an in order traversal of that tree. Your implementation should throw an IllegalArgumentException if the tree is null. Your implementation must implement the FlattenTree interface

For example a tree like:

 /|\
1 | 6
 /|\
5 4 9

would result in the list [1,5,4,9,6]. Your class must be named flatten.MyFlattenTree

Question 2:

Implement a method that, given a tree as a parameter, will return an in order traversal of that tree. Your implementation should throw an IllegalArgumentException if the tree is null. Your implementation must implement the FlattenTree interface

For example a tree like:

 /|\
1 | 6
 /|\
5 4 9

would result in the list [1,5,4,9,6]. Your class must be named flatten.MyFlattenTree

//------------------------------------------------------


//------------------------------------------------------


//------------------------------------------------------


//------------------------------------------------------


//------------------------------------------------------


package flatten;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MyFlattenTree<T> implements FlattenTree<T> {
 
 public List<T> flattenInOrder(Tree<T> tree) {
 if (tree == null)
 throw new IllegalArgumentException("Tree is null.");
 
 if (tree.get().isLeft()) {
 
 return Arrays.asList(tree.get().ifLeft( new Function<T, T>() { 
 
 public T apply(T p) {
 return p;
 }
 
 } ));
 
 } else {
 return tree.get().ifRight( new Function<Triple<Tree<T>>, List<T>>() {
 
 public List<T> apply(Triple<Tree<T>> p) {
 
 List<T> nodes = new ArrayList<T>();
 nodes.addAll(flattenInOrder(p.left()));
 nodes.addAll(flattenInOrder(p.middle()));
 nodes.addAll(flattenInOrder(p.right()));
 
 return nodes; //return all fetched nodes
 }
 } );
 
 } //end if
 
 } //end function
 
 public static void main(String[] args)
 {
 Tree<Integer> nodes = Tree.Node.tree(5, 4, 9);
 Tree<Integer> root = new Tree.Node<Integer>(Tree.Leaf.leaf(1), nodes, Tree.Leaf.leaf(6));
 MyFlattenTree<Integer> myFlattenTree = new MyFlattenTree<Integer>();
 
 System.out.println("Flattened tree: " + myFlattenTree.flattenInOrder(root));
 }
}

Java Interview - Missing level of technical depth (Flatten Tree)

They refused me by saying that"as we felt they didn't demonstrate the level of technical depth we're seeking from candidates"

Question 1 : Java Interview - Missing level of technical depth (Recursive to Iterative)

Question 3 : Java Interview - Missing level of technical depth (Common Ancestor)

Question 2: Implement a method that, given a tree as a parameter, will return an in order traversal of that tree. Your implementation should throw an IllegalArgumentException if the tree is null. Your implementation must implement the FlattenTree interface

For example a tree like:

 /|\
1 | 6
 /|\
5 4 9

would result in the list [1,5,4,9,6]. Your class must be named flatten.MyFlattenTree

//------------------------------------------------------

//------------------------------------------------------

//------------------------------------------------------

//------------------------------------------------------

//------------------------------------------------------

package flatten;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MyFlattenTree<T> implements FlattenTree<T> {
 
 public List<T> flattenInOrder(Tree<T> tree) {
 if (tree == null)
 throw new IllegalArgumentException("Tree is null.");
 
 if (tree.get().isLeft()) {
 
 return Arrays.asList(tree.get().ifLeft( new Function<T, T>() { 
 
 public T apply(T p) {
 return p;
 }
 
 } ));
 
 } else {
 return tree.get().ifRight( new Function<Triple<Tree<T>>, List<T>>() {
 
 public List<T> apply(Triple<Tree<T>> p) {
 
 List<T> nodes = new ArrayList<T>();
 nodes.addAll(flattenInOrder(p.left()));
 nodes.addAll(flattenInOrder(p.middle()));
 nodes.addAll(flattenInOrder(p.right()));
 
 return nodes; //return all fetched nodes
 }
 } );
 
 } //end if
 
 } //end function
 
 public static void main(String[] args)
 {
 Tree<Integer> nodes = Tree.Node.tree(5, 4, 9);
 Tree<Integer> root = new Tree.Node<Integer>(Tree.Leaf.leaf(1), nodes, Tree.Leaf.leaf(6));
 MyFlattenTree<Integer> myFlattenTree = new MyFlattenTree<Integer>();
 
 System.out.println("Flattened tree: " + myFlattenTree.flattenInOrder(root));
 }
}

Missing level of technical depth (Flatten Tree)

They refused me by saying that

as we felt they didn't demonstrate the level of technical depth we're seeking from candidates

Question 1 : Missing level of technical depth (Recursive to Iterative)

Question 3 : Missing level of technical depth (Common Ancestor)

Question 2:

Implement a method that, given a tree as a parameter, will return an in order traversal of that tree. Your implementation should throw an IllegalArgumentException if the tree is null. Your implementation must implement the FlattenTree interface

For example a tree like:

 /|\
1 | 6
 /|\
5 4 9

would result in the list [1,5,4,9,6]. Your class must be named flatten.MyFlattenTree






package flatten;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MyFlattenTree<T> implements FlattenTree<T> {
 
 public List<T> flattenInOrder(Tree<T> tree) {
 if (tree == null)
 throw new IllegalArgumentException("Tree is null.");
 
 if (tree.get().isLeft()) {
 
 return Arrays.asList(tree.get().ifLeft( new Function<T, T>() { 
 
 public T apply(T p) {
 return p;
 }
 
 } ));
 
 } else {
 return tree.get().ifRight( new Function<Triple<Tree<T>>, List<T>>() {
 
 public List<T> apply(Triple<Tree<T>> p) {
 
 List<T> nodes = new ArrayList<T>();
 nodes.addAll(flattenInOrder(p.left()));
 nodes.addAll(flattenInOrder(p.middle()));
 nodes.addAll(flattenInOrder(p.right()));
 
 return nodes; //return all fetched nodes
 }
 } );
 
 } //end if
 
 } //end function
 
 public static void main(String[] args)
 {
 Tree<Integer> nodes = Tree.Node.tree(5, 4, 9);
 Tree<Integer> root = new Tree.Node<Integer>(Tree.Leaf.leaf(1), nodes, Tree.Leaf.leaf(6));
 MyFlattenTree<Integer> myFlattenTree = new MyFlattenTree<Integer>();
 
 System.out.println("Flattened tree: " + myFlattenTree.flattenInOrder(root));
 }
}
Source Link
AAhad
  • 409
  • 1
  • 3
  • 10
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /