Microsoft Interview Question for SDE-2s


Country: United States
Interview Type: In-Person




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

static class Node {
		Node(int data) {
			this.data = data;
		}

		@Override
		public String toString() {
			return this.data + " ";
		}

		int data;
		Node left;
		Node right;
		Node next;
	}
	
	public static void main(String args[]) throws Exception {
		int arr[] = { 4, 2, 1, 3, 6, 5, 7 };
		Node root = builtTreeFromPreOrder(arr);
		System.out.println(root);
	}

	public static Node builtTreeFromPreOrder(int arr[]) {
		Stack<Node> stack = new Stack<Node>();
		Node root = new Node(arr[0]);
		stack.push(root);
		int index = 1;
		while (index < arr.length) {
			if (!stack.isEmpty() && stack.peek().data > arr[index]) {
				Node temp = new Node(arr[index]);
				stack.peek().left=temp;
				stack.push(temp);
				index++;
			} else {
				Node temp = null;
				while (!stack.isEmpty() && stack.peek().data < arr[index]) {
					temp = stack.pop();
				}
				if (temp != null) {
					Node temp1 = new Node(arr[index]);
					temp.right = temp1;
					stack.push(temp1);
					index++;
				}
			}
		}
		return root;
	}

- koustav.adorable September 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Psuedocode:
int index = 0;
function(A, min, max):
node = null;
if(A[index] between min and mx)
node= A[index]
index++
node.left = function(A, min,A[index])
node.right = function(A, A[index], max)

return node;

- sayantanc85 September 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Time Complexity O(n)
static Node constructTree(int[] pre, int size) {

                Node root = new Node(pre[0]);
                Stack<Node> stack = new Stack<Node>();

                stack.push(root);

                for (int index = 1; index < size; index++) {

                        Node temp = null;

                        while (!stack.isEmpty() && pre[index] > stack.peek().data) {
                                temp = stack.pop();
                        }

                        if (null != temp) {
                                temp.right = new Node(pre[index]);
                                stack.push(temp.right);
                        } else {
                                temp = stack.peek();
                                temp.left = new Node(pre[index]);
                                stack.push(temp.left);
                        }
                }

                return root;
        }

- undefined September 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Time Complexity O(n)
static Node constructTree(int[] pre, int size) {

                Node root = new Node(pre[0]);
                Stack<Node> stack = new Stack<Node>();

                stack.push(root);

                for (int index = 1; index < size; index++) {

                        Node temp = null;

                        while (!stack.isEmpty() && pre[index] > stack.peek().data) {
                                temp = stack.pop();
                        }

                        if (null != temp) {
                                temp.right = new Node(pre[index]);
                                stack.push(temp.right);
                        } else {
                                temp = stack.peek();
                                temp.left = new Node(pre[index]);
                                stack.push(temp.left);
                        }
                }

                return root;
        }

- Kapil September 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is O(n^2)

- samsamsamv2 September 15, 2017 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static TreeNode getTreeFromPreorder(int[] nums){
        Deque<TreeNode> deque = new ArrayDeque<>();
        if(nums==null || nums.length<1){
            return null;
        }
        
        TreeNode root = new TreeNode(nums[0]);
        deque.offer(root);
        for(int i=1;i<nums.length;i++){
            TreeNode node = new TreeNode(nums[i]);
            TreeNode temp = null;
            while(!deque.isEmpty()&&deque.peekLast().val<node.val){
                temp = deque.pollLast();
            }
            if(temp!=null){
                temp.right = node;
                deque.offer(node);
            }else{
                deque.peekLast().left = node;
                deque.offer(node);
            }
            
        }
        return root;
    }

- Anonymous September 29, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static TreeNode getTreeFromPreorder(int[] nums){
        Deque<TreeNode> deque = new ArrayDeque<>();
        if(nums==null || nums.length<1){
            return null;
        }
        
        TreeNode root = new TreeNode(nums[0]);
        deque.offer(root);
        for(int i=1;i<nums.length;i++){
            TreeNode node = new TreeNode(nums[i]);
            TreeNode temp = null;
            while(!deque.isEmpty()&&deque.peekLast().val<node.val){
                temp = deque.pollLast();
            }
            if(temp!=null){
                temp.right = node;
                deque.offer(node);
            }else{
                deque.peekLast().left = node;
                deque.offer(node);
            }
            
        }
        return root;
    }

- tiandiao123 September 29, 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