Reading std C lib header contents from command line:
echo "#include<string.h>" | cpp | grep "strstr" Displays strstr() function definition
Finding multiple char in a string, handle case where last char is unique
To find a number which occurs odd number of times from a list, make use of the XOR ‘^’ operator. The result will be the number which occurs for odd number of times.
Example: 3 XOR 5 XOR 3 = 5
Number reversal in python can be done as:
str(num)[::-1]
=> means that take all numbers, and stride is -1Kadane's Algorithm for Finding max sum of subarray in O(n) time
DP:
For any dynamic programming question, there are two ways to think about it:
- Top down approach (recursive+memoized)
- Bottom up DP - iterative approach
Measuring the complexity: Approach 1: Time for Number of subproblems O(1) ~ for fibonacci, n O(1) = n
For longest common subsequence:
Recursively: If last letter is same, then do
1 + lcs(a[m-1], b[n-1])
Perfectly balanced binary tree has
i. Median at root (odd numbers of entries) ii. Median as average of middle two elements (even entries)
Bit manipulation:
- Logical shift - drop sign
- Arithmetic shift - keep sign
- Find ith bit - left shift 1 by i bits, and then 'and':
x&(1<<i)
- Set ith bit - left shift 1 by i bits, and then 'or':
x | (1<<i)
- Clear ith bit - left shift 1 by i bits, take 1's complement, then 'and':
~(i<<i) & x
Use
string[i].isalnum()
for checking if a char in the string is alpha numeric or notIf numbers are stored as strings in array:
use
a[i].isdigit()
to check if number For negative numbers, we need to remove the '-' first:a[i].lstrip('-').isdigit*()
For finding difference in two strings, create dictionary with first string letter,
Use sets in python when want unique elements. Example: used in candy distribution
For finding the prime numbers, use Eratosthenes Sieve:
def SieveOfEratosthenes(n):Create a boolean array "prime[0..n]" and initializeall entries it as true. A value in prime[i] willfinally be false if i is Not a prime, else true.prime = [True for i in range(n+1)]p = 2while (p * p <= n):If prime[p] is not changed, then it is a primeif (prime[p] == True):Update all multiples of pfor i in range(p * 2, n+1, p):prime[i] = Falsep += 1Difference to remember in Least Common Substring and Subsequence:
Substring: continguous: 'aabbcd' - 'abbc'
Subsequence: not continguous: 'aabbcd' - 'abd'
Difference in matrices:
Substring: if letters match - put diagonal+1
if letters don't match, put 0
Subsequence: if letters match - put diagonal+1
if letters don't match, put max(top, left)
Python list splicing takes O(k) time, where k->len of spliced array
Use
enumerate()
instead ofrange(len(something))
when using indices multiple timesConversion in python:
format(num, "0b") -> binary, format(num, "0x") -> hex
Check if power of 2: simply take math.log(n,2) and see it is int. i.e:
math.log(n,2) %1 == 0
For length of substring problems, use sliding window for O(n) complexity.
Problems which can be solved with this:1. length of longest substring without repeating charQuicksort's partition function can be used in many problems such as the dutch flag (red, white, blue) problem.
def parition(pivot,a):i = -1j = 0while j <= pivot:if a[j] <= a[pivot]:swap(a[i+1],a[j])j += 1i += 1else:j += 1For the flag problem, there are 3 parts we want to divide intoso modify partition function as follows:def modified_partition(a):low = 0mid = 0high = len(a)-1while mid <= high:if a[mid] == 1:mid += 1if a[mid] == 0:swap(a[low],a[mid])mid += 1low += 1if a[mid] == 2:swap(a[mid],a[high])high -= 1For storing the value of each level in subarray during tree traversal, store (root, level) in q/stack
Example:Binary Tree Level Order Traversal II Problem:def levelOrderBottom(self, root):""":type root: TreeNode:rtype: List[List[int]]"""if not root:return []res = []q = list()level = 0q.append((root,level))self.bfs(q, res)return res[::-1]def bfs(self, q, res):while q:curr, level = q.pop(0)if level == len(res):res.append([])res[level].append(curr.val)if curr.left:q.append((curr.left, level+1))if curr.right:q.append((curr.right, level+1))return res
Last active
March 20, 2018 04:37
-
-
Save mihirchakradeo/0e9deca3806828dc8ed4ba6ed0eaac78 to your computer and use it in GitHub Desktop.
Interview Prep Notes
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment