Apple Interview Question for Software Engineers


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

Not exactly proud of it, but:

def BST : {
    $$ : def(){
        $.root = null
    },
    add_node : def(v){
      new_node = {'v' : v , 'l' : null , 'r' : null }  
      if ( empty($.root) ){
          $.root = new_node
          return $.root 
      } 
      parent = $.root
      current = v < parent.v  ? parent.l : parent.r 
      while ( current != null ){
         parent = current
         current = v < parent.v  ? parent.l : parent.r   
      } 
      if ( v < parent.v ){
          parent.l = new_node
      } else if ( parent.v < v ){
          parent.r = new_node
      }    
    },
    find_abc : def(key){
      current = $.root 
      path = ''
      while ( current != null && current.v != key ){
         if (  key < current.v ){
            path += '0'  
            current = current.l 
         } else if ( current.v < key ){
            path += '1'
            current = current.r 
         }
      }
      (!empty(current) && current.v == key)? path : 'Not Found'
    }
}
//now play 
bst = new ( BST )
values = [5,2,8,3,6,9,1]
// add nodes 
for ( v : values ){ bst.add_node(v) }
keys =  [6, 1, 10, 2]
// print ABC ... 
for ( v : keys ){ printf( '%s -> %s%n', v, bst.find_abc(v) ) }

- NoOne June 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public String bstSearch(TreeNode<Integer> node , int num){
		if (node == null){
			return "NotFound";
		}
		if (node.val == num){
			return "Undefined";
		}
		String str = bst(node, num);
		return str;
	}
	
	public String bst(TreeNode<Integer> node, int num){
		if (node == null){
			return "NotFound";
		}
		if (node.val == num){
			return "found";
		}
		if (num < node.val){
			String str1 = bst(node.left, num);
			if (str1.equals("NotFound")){
				return str1;
			}
			else if (str1.equals("found")){
				return "0";
			}
			else {
				return "0"+str1;
			}
		}
		if (num > node.val){
			String str1 = bst(node.right, num);
			if (str1.equals("NotFound")){
				return str1;
			}
			else if (str1.equals("found")){
				return "1";
			}
			else {
				return "1"+str1;
			}
		}
		return null;
	}


class TreeNode<T> {
	      T val;
	      TreeNode left;
	      TreeNode right;
	      TreeNode(T x) { val = x; }
 }

- Kush June 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution in python

class node(object):
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None
    
    
def buildABCString(root, value):
    res  = ""
    if root.value == value: return "Undefined"
    node = root
    while node != None:
        if node.value == value: return res
        elif value > node.value:
            res += "1"
            node = node.right
        else:
            res += "0"
            node = node.left
    return "Not Found"

- Fernando June 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public String bstNotation(int num) {
		if (root == null) return "Not Found";
		Node curr = root;
		StringBuilder notation = new StringBuilder();
		if(root.data==num)
			return "Undefined";
		
		while (curr != null) {
			if (num<curr.data) {
				curr = curr.left;
				notation.append("0");
			} else if (num>curr.data) {
				curr = curr.right;
				notation.append("1");
			} else if (num==curr.data) {
				return notation.toString();
			} 
		}
		return "Not Found";
	}

- MS June 24, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More