Max & Min arrays Flashcards

1
Q

Reverse Integer

A
  1. Normal reverse operation using % * and /
    int newResult = result * 10 + x % 10;
  2. Reconstruct the reverse number and check for equality
    if(((newResult - x % 10) / 10) != result) return 0;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Longest palindrome substring

A
  1. Expand around corner, odd and even, maxLen and llo
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

No of ways of decode

A
  1. Classic dp with recursion and memoisation

2. Do with one char and two chars

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

K Frequent Element

A
  1. Hashmap to store the occurence
  2. Min heap to store the least occurence map.get(a) - map.get(b)
  3. Add all the el to heap, if size > k poll
  4. Finally poll the last remaining element
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Max path sum

A
  1. Recursive
  2. max(0, leftSum || rightSum)
  3. maxPath = inOrder || maxSum
  4. return max(left, right) + node.val;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Merge sorted LL

A
  1. l1 = mergeSort(l1.next, l2)

2. Use the same list, no need to create new LL

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

Word search in a adjacent matrix

A
  1. Use the count var to keep track of the current idx of the word
  2. Handle the visited nodes, by making empty and undoing at the end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Valid palindrome of sentences with punctuations

A
  1. Use two pointer
  2. Use character.isLetterOrDigit to consider digits as well
  3. Check for charAt match
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Find first and last of duplicate el in sorted array

A
  1. Two BST search
  2. First to search for the firstIndex and second is for searching the endIndex
  3. If target found for the first time, check whether its starting else checking on the left side
  4. Do the same of the ending index
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Find peak element in array

A
  1. BST
  2. If current > current + 1 search left
  3. If current < current + 1 search right
  4. end at start == end return start
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

IsCelebrity

A
  1. for to get the possible celebrity candidate

2. If celebrity exists then the possible candidate will be compared against all other candidates

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

Is tree symemtry

A

t1.val == t2.val
&& isSymmetric(t1.right, t2.left)
&& isSymmetric(t1.left, t2.right);

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

Search in 2D matrix 1

A
  1. Consider the whole matrix as an increasing order of array
  2. pivotIdx = mid
  3. pivotValue = matrix[idx/n][idx%n]
  4. Rest all the binary logic
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Longest consecutive sequence

A
  1. Store all the el in hashset
  2. The logic is to find the the el - 1 not present in the set, and in the does a forloop to check whether from current no + 1 are consective and check for maxlen
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Perfect square

A
  1. dp, with base[0] = 0 and fill the rest with integer max;
  2. Iterate for i to n
  3. iterate for every sqr j = 1 ; j *j <= i; j++
  4. dp[i] = min(dp[i], dp[i - j * j] + 1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

inOrderSuccessor

A
while(root != null) {
            if(p.val >= root.val) {
                root = root.right;
            } else {
                successor = root;
                root = root.left;
            }
        }