Microsoft Interview Question for Interns


Country: United States




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

what is an unordered tree?

- veeru February 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Node
	{
		int data;
		Node left, right;
		
		public Node(int key)
		{
			data = key;
			left = null;
			right = null;
		}
	}
	
	class unordered 
	{
		Node root;
		
		unordered(int key)
		{
			root = new Node(key);
		}
		unordered()
		{
			root=null;
		}
		
		public void uotollrec(Node roo, List<Integer> ll)
		{
			
			if(roo==null)
				return;
			ll.add(roo.data);
			uotollrec(roo.left,ll);
			uotollrec(roo.right,ll);
			
		}
	}
	
	class BinarySearchTree
	{
		Node root;
		
		BinarySearchTree() {
			root = null;
		}
		
		void addbinary(int item)
		{
			root = addrec(root,item);
		}
		
		Node addrec(Node root, int item)
		{
			if(root == null)
			{
				root = new Node(item);
				return root;
			}
			if (root.data>item)
				root.left=addrec(root.left,item);
			else
				root.right=addrec(root.right,item);
			
			return root;
		}
		
		void trav()
		{
			traverse1(root);
		}
		void traverse1(Node roo)
		{
			
			if(roo!=null)
			{
			System.out.println(roo.data);
			traverse1(roo.left);
			traverse1(roo.right);
			}
			
		}
	}
	
	public class first
	{
		static List<Integer> ll = new ArrayList<Integer>();
		BinarySearchTree bst = new BinarySearchTree();
		
		public void uotobinary(unordered uo)
		{
			int r;
			unordered u1 = new unordered();
			u1.uotollrec(uo.root,ll);
			Collections.sort(ll);
			if(ll.size()%2==0)
				r = ll.size()/2-1 ;
			else
				r = (ll.size()/2);
			bst.addbinary(ll.get(r));
			
			for(int i = r-1; i>=00;i--)
				bst.addbinary(ll.get(i));
			
			for(int i = r+1; i<ll.size() ; i++)
				bst.addbinary(ll.get(i));
			
			bst.trav();
			
		}	
	
	public static void main(String[] args) 
    {
		first f1 = new first();
    	unordered uo = new unordered();
   		uo.root = new Node(7);
   		uo.root.left=new Node(6);
   		uo.root.right=new Node(5);
   		uo.root.left.left=new Node(4);
   		uo.root.left.right=new Node(3);
   		uo.root.right.left=new Node(2);
   		uo.root.right.right=new Node(1);   		
   		
   		f1.uotobinary(uo);   		
    }
}

- PalakMundra March 28, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Node
	{
		int data;
		Node left, right;
		
		public Node(int key)
		{
			data = key;
			left = null;
			right = null;
		}
	}
	
	class unordered 
	{
		Node root;
		
		unordered(int key)
		{
			root = new Node(key);
		}
		unordered()
		{
			root=null;
		}
		
		public void uotollrec(Node roo, List<Integer> ll)
		{
			
			if(roo==null)
				return;
			ll.add(roo.data);
			uotollrec(roo.left,ll);
			uotollrec(roo.right,ll);
			
		}
	}
	
	class BinarySearchTree
	{
		Node root;
		
		BinarySearchTree() {
			root = null;
		}
		
		void addbinary(int item)
		{
			root = addrec(root,item);
		}
		
		Node addrec(Node root, int item)
		{
			if(root == null)
			{
				root = new Node(item);
				return root;
			}
			if (root.data>item)
				root.left=addrec(root.left,item);
			else
				root.right=addrec(root.right,item);
			
			return root;
		}
		
		void trav()
		{
			traverse1(root);
		}
		void traverse1(Node roo)
		{
			
			if(roo!=null)
			{
			System.out.println(roo.data);
			traverse1(roo.left);
			traverse1(roo.right);
			}
			
		}
	}
	
	public class first
	{
		static List<Integer> ll = new ArrayList<Integer>();
		BinarySearchTree bst = new BinarySearchTree();
		
		public void uotobinary(unordered uo)
		{
			int r;
			unordered u1 = new unordered();
			u1.uotollrec(uo.root,ll);
			Collections.sort(ll);
			if(ll.size()%2==0)
				r = ll.size()/2-1 ;
			else
				r = (ll.size()/2);
			bst.addbinary(ll.get(r));
			
			for(int i = r-1; i>=00;i--)
				bst.addbinary(ll.get(i));
			
			for(int i = r+1; i<ll.size() ; i++)
				bst.addbinary(ll.get(i));
			
			bst.trav();
			
		}	
	
	public static void main(String[] args) 
    {
		first f1 = new first();
    	unordered uo = new unordered();
   		uo.root = new Node(7);
   		uo.root.left=new Node(6);
   		uo.root.right=new Node(5);
   		uo.root.left.left=new Node(4);
   		uo.root.left.right=new Node(3);
   		uo.root.right.left=new Node(2);
   		uo.root.right.right=new Node(1);   		
   		
   		f1.uotobinary(uo);   		
    }
}

- PalakMundra March 28, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Node
	{
		int data;
		Node left, right;
		
		public Node(int key)
		{
			data = key;
			left = null;
			right = null;
		}
	}
	
	class unordered 
	{
		Node root;
		
		unordered(int key)
		{
			root = new Node(key);
		}
		unordered()
		{
			root=null;
		}
		
		public void uotollrec(Node roo, List<Integer> ll)
		{
			
			if(roo==null)
				return;
			ll.add(roo.data);
			uotollrec(roo.left,ll);
			uotollrec(roo.right,ll);
			
		}
	}
	
	class BinarySearchTree
	{
		Node root;
		
		BinarySearchTree() {
			root = null;
		}
		
		void addbinary(int item)
		{
			root = addrec(root,item);
		}
		
		Node addrec(Node root, int item)
		{
			if(root == null)
			{
				root = new Node(item);
				return root;
			}
			if (root.data>item)
				root.left=addrec(root.left,item);
			else
				root.right=addrec(root.right,item);
			
			return root;
		}
		
		void trav()
		{
			traverse1(root);
		}
		void traverse1(Node roo)
		{
			
			if(roo!=null)
			{
			System.out.println(roo.data);
			traverse1(roo.left);
			traverse1(roo.right);
			}
			
		}
	}
	
	public class first
	{
		static List<Integer> ll = new ArrayList<Integer>();
		BinarySearchTree bst = new BinarySearchTree();
		
		public void uotobinary(unordered uo)
		{
			int r;
			unordered u1 = new unordered();
			u1.uotollrec(uo.root,ll);
			Collections.sort(ll);
			if(ll.size()%2==0)
				r = ll.size()/2-1 ;
			else
				r = (ll.size()/2);
			bst.addbinary(ll.get(r));
			
			for(int i = r-1; i>=00;i--)
				bst.addbinary(ll.get(i));
			
			for(int i = r+1; i<ll.size() ; i++)
				bst.addbinary(ll.get(i));
			
			bst.trav();
			
		}	
	
	public static void main(String[] args) 
    {
		first f1 = new first();
    	unordered uo = new unordered();
   		uo.root = new Node(7);
   		uo.root.left=new Node(6);
   		uo.root.right=new Node(5);
   		uo.root.left.left=new Node(4);
   		uo.root.left.right=new Node(3);
   		uo.root.right.left=new Node(2);
   		uo.root.right.right=new Node(1);   		
   		
   		f1.uotobinary(uo);   		
    }
}

- PalakMundra March 28, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Node
	{
		int data;
		Node left, right;
		
		public Node(int key)
		{
			data = key;
			left = null;
			right = null;
		}
	}
	
	class unordered 
	{
		Node root;
		
		unordered(int key)
		{
			root = new Node(key);
		}
		unordered()
		{
			root=null;
		}
		
		public void uotollrec(Node roo, List<Integer> ll)
		{
			
			if(roo==null)
				return;
			ll.add(roo.data);
			uotollrec(roo.left,ll);
			uotollrec(roo.right,ll);
			
		}
	}
	
	class BinarySearchTree
	{
		Node root;
		
		BinarySearchTree() {
			root = null;
		}
		
		void addbinary(int item)
		{
			root = addrec(root,item);
		}
		
		Node addrec(Node root, int item)
		{
			if(root == null)
			{
				root = new Node(item);
				return root;
			}
			if (root.data>item)
				root.left=addrec(root.left,item);
			else
				root.right=addrec(root.right,item);
			
			return root;
		}
		
		void trav()
		{
			traverse1(root);
		}
		void traverse1(Node roo)
		{
			
			if(roo!=null)
			{
			System.out.println(roo.data);
			traverse1(roo.left);
			traverse1(roo.right);
			}
			
		}
	}
	
	public class first
	{
		static List<Integer> ll = new ArrayList<Integer>();
		BinarySearchTree bst = new BinarySearchTree();
		
		public void uotobinary(unordered uo)
		{
			int r;
			unordered u1 = new unordered();
			u1.uotollrec(uo.root,ll);
			Collections.sort(ll);
			if(ll.size()%2==0)
				r = ll.size()/2-1 ;
			else
				r = (ll.size()/2);
			bst.addbinary(ll.get(r));
			
			for(int i = r-1; i>=00;i--)
				bst.addbinary(ll.get(i));
			
			for(int i = r+1; i<ll.size() ; i++)
				bst.addbinary(ll.get(i));
			
			bst.trav();
			
		}	
	
	public static void main(String[] args) 
    {
		first f1 = new first();
    	unordered uo = new unordered();
   		uo.root = new Node(7);
   		uo.root.left=new Node(6);
   		uo.root.right=new Node(5);
   		uo.root.left.left=new Node(4);
   		uo.root.left.right=new Node(3);
   		uo.root.right.left=new Node(2);
   		uo.root.right.right=new Node(1);   		
   		
   		f1.uotobinary(uo);   		
    }
}

- Palak.1009 March 28, 2018 | 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