Sliding Window Flashcards
What are the various approaches of Sliding Window ?
- Sliding Window with fixed Size 2. Sliding window with variable size 3. Sliding window with hashmap. 4. Sliding window with tracking of maxValue .
- Window with a substring function
- Window stored ina dequeue
- How can we detect sliding window ?
- Whenever we have Sequential Values
- Substring problems which is again sequential values.
- Longest sub array type of problems
- Contiguous elements in a linkedlist
- How to get the number of elements between 2 index
highindex - lowIndex +1
- What is the general format ?
Common errors in 2 pointer method ?
- While iterating you might miss an element when you do it while(left<right>
</right><li>last pointer should be in arr.length-1 not arr.length</li>
</right>
Whats the gotcha for sort in js ?
The sort is alphabetic , so we need to pass the function to make it sort properly
What does in-place replacement and constant space signify ?
It should immediately indicate swapping
Whenever we need to generate integer from strings , add (sum), multiply or have any integer operations. What is a potential pitfall
- Always think of max bytes .
- it could be individual numbers are integers but when you sum them , you get the bytes overflow
What are the properties of XOR ?
- taking XOR of a number with itself returns 0, e.g.,
1 ^ 1 = 0
29 ^ 29 = 0
Taking XOR of a number with 0 returns the same number, e.g.,
1 ^ 0 = 1
31 ^ 0 = 31
XOR is Associative & Commutative, which means:
(a ^ b) ^ c = a ^ (b ^ c)
a ^ b = b ^ a
How to invert bytes of a number ?
number ^ complement = all_bits_set
number ^ number ^ complement = number ^ all_bits_set
0 ^ complement = number ^ all_bits_set
complement = number ^ all_bits_set
How to store a binary tree ?
If parent is at i
LeftChild 2i
RIghtChild2i+1
Level order traversal , keep missing rows blank
If the height of the binary tree is h , how many element in full bionary tree ?
2^(h+1)-1
Whats hieght of a binary tree ?
log N
What should immeditaley come to mind when you see hash map and charachetrs
Hashmap of 26 charachters
Do you need repetitions ?
During DFS , whats the complexity?
, it is the size of the tree
What are the 2 key terms of tree traversal ?
- Visiting a vertex - landing on a vertex
- Exploration of a Vertex - Visting all its adjacent vertices
Difefrence between BFS and DFS
- In BFS we will explore a vertex then go to next vertex for exploration
- In DFS , once visited the new vertex , we suspend the vertex and start explorarion
- Use a Queue - DFS
- Push root into the que , start exploring its children and push them to the queue till nothing is left
- Pop and follow the same approach
- Level Order Traversal
- Use a stack - DFS
- preorder traversal
- print when you visit
What is the complexity of inserting into the a sorted array ?
Inserting a number in a sorted list will take O(N) time if there are ‘N’ numbers in the list. This insertion will be similar to the Insertion sort.
What are the functions a heap ?
Push()
pop()
length
peek()
What is a balanced binary search tree ?
If the depth of 2 subtrees of any node differ no more than 1
Whats the time complexity of knapsack
- brute force 2^n - (2^n)+(2^n)−1
- DP - O(NC)
DP , when to pass count , and when to return count ?
- If the count needs to be reset as Longest common substring , its best to pass the count. as well as return it . Because count from parent culd be reset.
- If the count stays same and does not need to be reset , we should be able to return count and add the current parets count to the child .
What should we remember while passing values into recursion ?
- See if we can pass values which are function of index rarther than the value itself .
- instead of sending prev values , can you send the prev index ?
What are the steps for topological sort ?
- Make an adjecancy list
- reate an indegrees array ( index ->count of in edges)
- Find sources, for in degrees which are 0 and push them tinto queue
- Start the top sort process
- Keep running till queue is empty
- Dequeue an element and put it into sorted array
- Get all the child vertex from the adjacanecy list for that element
- For each of child vertex
- Decrease indegress by 1
- if indegrees of any child drops to 0 , enqueue it.
- The sorted list is topologically sorted.
- if length of sorted list is less than vertex , there is a cycle