Tech Interview
Check if a binary tree is BST or not
- (Smaller than parent) left <- Node -> right (Bigger than parent)
- Node starts with (-infinity, infinity)
Clone a doubly linked list which has a next and an arbitrary (random) pointer
- Make new list and point to new node
- Copy arbitrary from new node to original node
- Copy it to new node
Connect nodes at the same level in a binary tree
- Make a queue
- Put node and null node (to separate)
- Another level, put nodes and null node at the end
- O(n)
Remove duplicates from a string and arrange its letters in order
- Self balancing Binary search tree and the elements inserted in a set are unique and automatically sorted.
- If we put it in the set, we are not going to accept same character.
- O(blogs)
Given a rolated array which is sorted search for an element in it.
- Since array is rotated, we need to divide it into two sub arrays.
- Go to first sub array and compare mynum with first element in subarray
- If it is smaller than first element in subarray, we are going to switch subarray
- Use binary search
Two of the nodes of a BST are swapped correct the BST
- We do Inorder traversal (left to the right)
- We now know which value is bigger than parent at the left and which value is smaller than parent at the right
Detect Cycle in a linked list
- We will take fast pointer and slow pointer
- Fast pointer move twice faster than slow pointer
- If those two pointer meet at some point, we can judge there is a cycle
Find the Least Common Ancestor (LCA) of a binary tree
- We start from the top node, look for both left and right
- Make search result to parent (Null,a)
- If it makes (a,b) then we found LCA
- Pass(L,Null) to top node
Check if a binary tree is height balanced
- If left sub tree and right sub tree height difference is less than 1, It is balanced.
Given an unsorted array of size ānā such that only one element occurs twice. All the elements are from 1 to n. Find the missing element.
- Put array in to XOR and make another XOR that contains 1 to n
- And we cancel the element from both side if it matches, then we can get missing element
- Duplicate element XOR missing element