DSA - Interview Bit Flashcards
Bubble sort
Keep comparing two elements and keep the higher element to the right. Keep shifting to the end of the array.
Result: The biggest element is put to the end of the array, followed by the next biggest and so on..
Insertion sort
Pick an element and keep swapping (shifting the array) with the adjacent element until it reaches its correct position.
Result: The left half of the array is sorted at any point in time. And the right half contains the unsorted array.
Selection sort
Find/select the min element from the array and place it at the beginning of the array. Select the next smallest element and place it in the second position and so on..
Result: The left half contains the sorted array at any point in time.
Check if the tree is a valid BST
Define a range on each node and check if its value lies in that range
Zig-zag level order tree traversal
Use 2 stacks
SUBSET-SUM PROBLEM
Find a subset of an array which equals sum = k
DP: Element of choice = the number is included in the subset or not
Longest contiguous subarray with equal number of 0s and 1s
Replace all 0s with -1.
Compute and store prefix sum.
If the prefix sum value at any two indices is equal, it means the number of 0s and 1s between those numbers is equal, and which is why they added up to zero.
Use hashmap {sum: min_index} to store the prefix sum as you traverse the array and maintain a maxlen variable.
REMEMBER: hashmap[0] = -1
The prefix sum is 0 at the start. We need to store this because if at any later index the prefix sum is 0, and we want to match with the min_index where we encountered 0 previously using the hashmap, that will be -1th index (where the sum was 0).
Max element in every k elements of an array
Double-ended queue
Merge overlapping intervals
https://leetcode.com/problems/merge-intervals/submissions/
Given a sorted array of integers, find the number of occurrences of a given target value.
If the target is not found in the array, return 0
Linear scan - O(n)
BINARY SEARCH - O (log n)
https://www.interviewbit.com/problems/count-element-occurence/
DP solving techniques
CodeCamp + Scaler academy
Questions will be like:
- In how many ways can you … ? Find number of substrings/arrays/sets … such that … ?
- Maximize/Minimize x
- Optimal substructure: Express the bigger problem into smaller problems. Look for the solution to smaller problems and see how to build the solution for the bigger problem = RECURSION.
- Overlapping subproblem: Reuse the previous states by storing them = DYNAMIC PROGRAMMING
- What does each state represent?
- What parameters are required to describe each state?
- Element of choice
ANS: Create a recursive equation f(n) and use memorization.
Use top-down approach to create the base cases and recursive equation.
In case 1) . ‘n’ will be some target value given in the question.
eg: In how many ways can you create the sum (= n) from an array (subset problem),
In how many ways can you travel from the starting point to n.
In case 2) ‘n’ will not be given explicitly in the question. You will take ‘n’ as the length of the given problem (array, matrix etc.)
eg: Maximize robber sum from the given houses. Here n will become the number of given houses
DP problems:
- Stairs problem
- Coin change
- Number of ways to reach (n,m) in a matrix
- Min cost path in matrix
- Party pairs problem
- Subset problem (and its variants)
https://www.scaler.com/academy/mentee-dashboard/classes
Find the kth smallest element in an array
Constraints: No extra space, cannot modify the array
More specific: Find median of an array
(Here, k = N/2)
- Sort array and return the middle element
- Use modified Binary search
The median will have exactly N/2 elements lesser than it.
Count the number of elements lesser than mid (lt) and number of elements equal to mid (eq).
Binary Search Main Idea
Given a random number x, can I decide if my answer will definitely be >x or
Find the missing number in an array of given range [1,n]
- Sum of first n natural numbers - Sum of array
= n(n+1) / 2 - sum(arr) - XOR
a = Element wise XOR all elements of array
b = Element wise XOR of numbers between [1,n]
Missing number = a^b
https://www.geeksforgeeks.org/find-the-missing-number/