DSA - Interview Bit Flashcards

1
Q

Bubble sort

A

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..

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Insertion sort

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Selection sort

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Check if the tree is a valid BST

A

Define a range on each node and check if its value lies in that range

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Zig-zag level order tree traversal

A

Use 2 stacks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

SUBSET-SUM PROBLEM

Find a subset of an array which equals sum = k

A

DP: Element of choice = the number is included in the subset or not

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Longest contiguous subarray with equal number of 0s and 1s

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Max element in every k elements of an array

A

Double-ended queue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Merge overlapping intervals

A

https://leetcode.com/problems/merge-intervals/submissions/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

Linear scan - O(n)
BINARY SEARCH - O (log n)

https://www.interviewbit.com/problems/count-element-occurence/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

DP solving techniques

CodeCamp + Scaler academy

A

Questions will be like:

  1. In how many ways can you … ? Find number of substrings/arrays/sets … such that … ?
  2. 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
  1. What does each state represent?
  2. What parameters are required to describe each state?
  3. 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

DP problems:

  1. Stairs problem
  2. Coin change
  3. Number of ways to reach (n,m) in a matrix
  4. Min cost path in matrix
  5. Party pairs problem
  6. Subset problem (and its variants)
A

https://www.scaler.com/academy/mentee-dashboard/classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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)

A
  1. Sort array and return the middle element
  2. 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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Binary Search Main Idea

A

Given a random number x, can I decide if my answer will definitely be >x or

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Find the missing number in an array of given range [1,n]

A
  1. Sum of first n natural numbers - Sum of array
    = n(n+1) / 2 - sum(arr)
  2. 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/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Trapping rainwater

A
  1. Create Left max and Right max arrays
    O(n) time, O(2n) space
  2. Instead of storing the left max and right max of every element, compute them in the loop.
    O(n) time, O(1) space
17
Q

Rotate an array k times

A
  1. Rotate one element at a time. For each of the k elements, n elements are shifted.
    O(n*k) time, O(1) space
  2. Use temp array
    O(n) time, O(k) space
  3. Reverse the original array. Reverse arr[:k] and arr[k+1:]
    O(n) time, O(1) space
18
Q

Print tree in vertical order

A

Leetcode

19
Q

Count Sorted Vowel Strings

Given an integer n, return the number of strings of length n that consist only of vowels (a, e, i, o, u) and are lexicographically sorted.

A

https://leetcode.com/problems/count-sorted-vowel-strings/discuss/918760/Dynamic-Programming-Python-100-Explanation-%2B-code

20
Q

Perfect Sum Problem (Print all subsets with given sum)

A

https://www.geeksforgeeks.org/perfect-sum-problem-print-subsets-given-sum/

21
Q

Linked List important points

A

Most problems require you to maintain 2 pointers: prev, current
prev = None
current = head
And within a while loop current != None, we will maintain nextNode pointer = current.next

Conditions are checked on the current node

  1. After deleting a node (prev.next = current.next), prev is not moved/updated (prev = current) in the while loop because we need a reference to the prev of the deleted node in order to link it to the remaining list. Only current is moved/updated (current = current.next), prev stays as it is.
  2. Take care of null pointers when accessing node.next
    Use if-else wherever appropriate.
    node.next if node else None
3. Base cases
        # empty list or list with only one node
        if head == None or head.next == None:
            return False
  1. Fast and slow pointers - used to find middle element of the list, cycle detection, kth node from end (variant of fast-slow),
  2. Double check while loop condition of what you want (while current != None OR while current.next != None)