-
Among all pairs find sum of bits that are different. link
Trick: For each bit scan all the elements in array. O(n.64) -
Find total number of continuous subarrays whose sum equals to k. geeksforgeeks leetcode
Trick: If integers are non-negetive then windowing solution.
Otherwise create sums array and use hashmap.
Variant-sum is multiple of k create sums array and use hashmap to find if the same remainder occurred previously. -
Maximum subarray problem leetcode
Trick: Start scanning for left, keep adding numbers in your result and maintain current sum. As soon the current_sum becomes negative => result.empty() and repeat above algorithm from next index. -
Rearrange string, same characters appear atleast d distance apart. geeksforgeeks
Trick: Sort by frequency(use heap, have to remain sorted at all times)
. Maintain lastd
characters in a set. Always print most frequent character which is not in the set. -
Merge Interval. Given a sorted list of non-overlapping intervals, merge a new interval in that list. interview-bit
Trick: Write a function to check overlap between two intervals.
Then scan the intervals and fit the new interval at its location if it overlaps with some intervals.
edge cases: The new interval may not overlap with any of the intervals in the list and can be at the start or at the end. -
Given an array of integers, find max product of 3 elements. interview-bit
Trick Sort the list. cases:
(i) No positive number and no zero present in list. (ii) No positive number but zero is in list. (iii) Two positives and one negative. (iv) Two or more negatives and one or more positives. -
Given a sorted array, remove duplicates in place. interview-bit
Trick Maintain two pointers.
- Sort Linked list in O(nlogn) time and constant space. interview-bit
Trick Use merge sort and recursion. Find mid point, sort two lists and merge them.(Technically recursion is not constant space solution)
-
Given file system in \t delimited string, find longest absolute path of a file. link
Trick: Append to the stack while entering a directory and pop while leaving. -
Decode String. s = "3[a]2[bc]", return "aaabcbc". link
Trick: Use recursion, or can be done iteratively using stack also.
-
Replace adjacent digits such that result is smallest possible. link solution
Trick: Find pattern (high, mid, low) => (high, low). Otherwise replace least significant digit. -
Remove duplicates from the sting such that remaining string has smallest lexicographical ordering. leetcode
Trick: Maintain locations of all chars in an array. Try to print 'a' by testing if all characters on left of 'a' also have a duplicate on right side of a. Then do this for remaining characters. -
Kth row of pascal triangle. Interview-bit
Trick: Kth row will have k+1 elements if K starts from 0.nC(r+1) = nCr * (n-r) / (r+1)
-
Minimum number of swaps required for arranging pairs adjacent to each other.geeksforgeeks
Tricks: Start from left. Two possible options:
a. Either swap 2nd element with the pair of 1st
b. Swap 1st element with the pair of 2nd element. -
Given string and a dict of words, return true if string can be broken into words in dict. Word Break Problem interview-bit
Trick-1 Start from reverse to prevent maximum recursion depth issue. Trick-2 Store lengths of words in dict and run a loop over these lengths.
def solve(self, start_index):
if start_index in self.my_map:
return self.my_map[start_index]
for length in self.lengths:
if start_index + length > self.length:
continue
word = self.string[start_index:start_index+length]
if start_index + length == self.length and word in self.dict:
self.my_map[start_index] = 1
return 1
elif start_index + length < self.length:
if word in self.dict and self.solve(start_index+length):
self.my_map[start_index] = 1
return 1
self.my_map[start_index] = 0
return 0
- Given a jumps array, reach last point in minimum jumps. interview-bit
Can be solved using DP also.
Trick Stand at indexstart_index = 0
. Keep a variablemax_reachable_index
. Scan fromstart_index
tomax_reachable_index
and now stand at index for whichmax_reachable_index
is maximum.