- Python Basics Cheat Sheet
- Python Cheat Sheet
- Python Coding Questions Interview
- Python Cheat Sheet Download
- Ask questions about the problem to clarify it.
- Don’t jump into the code, think and discuss.
- Don’t stay muted for a while, clarify when you are thinking, “Can I think for a second?”.
- Think out loud.
- Multiply the examples.
- List the corner cases.
- Ask for approval before coding, “Does it seem like a good strategy ?” “Should I start coding ?”
- Can sorting input data help me ?
- Can splitting input data help me ?
- Can a dictionary help me ?
- Can multiple pointers help me ?
- Can a frequency array help me ?
- Can multiple pass help me ?
- Can case-based reasoning help me ?
- Is there an ends here/with property ?
00:00 Let’s go through what you learned in this course. You learned a bunch of different concepts, and it’s always good to revisit what you learned. It’s also good to go through them and maybe make a cheat sheet or put them somewhere so you don’t forget. 00:11 I have an interview prep document where I’ve written a lot of this down, just so I don’t forget. Python Coding Interview Questions And Answers 2021. Here Coding compiler sharing a list of 35 Python interview questions for experienced. These Python questions are prepared by expert Python developers.This list of interview questions on Python will help you to crack your next Python job interview. All the best for your future and happy python learning.
- Checking if $i in Stack$ in $O(1)$: just keep updated an “is_in” array
is_in_stack
whereis_in_stack[i]
tells whether $i in Stack$. - Sets with fewer than 64 possible items can be coded as integers.
- For singly linked lists, creating 2 pointers $n$ nodes apart may be handy. It is also applicable to every sequence of elements. (find the middle / $n^{th}$ from the end, a cycle…)
- Use infinite boundaries
float('inf')
ormath.inf
. - Use frequency arrays when counting letters. A counting sort might also prove useful (as it is linear for frequencies) later on.
- To find $a$ and $b$ in a array so that $a+b = c$, save every seen number in a set $S$ and for every new $i$, check if $c-i in S$.
Python Basics Cheat Sheet
sorted ➜ Bisection search
iterative DFS ➜ stack
BFS ➜ queue
- Need to keep track of the depth? → 2 queues
Optimal substructure ➜ Dynamic programming to compute all the optimal solution of size $0 ldots n$ and deduce the solution.
- when it is possible to craft the optimal solution for an instance size $n$ using the solution to instances of size $i_1, ldots, i_p < n$.
- Can be on a pair of parameters, solution of size $(n, k)$ can be deduced from solution of size $(i, j)$ where $i leq n$ and $j leq k$.
Examples: subarray, knapsack, longest substring
Do It Yourself
- I've been wanting to update the Beginner's Python Cheat Sheets that accompany Python Crash Course for a while now. I finally made time over the last month to go through the entire set. I originally developed the sheets in Word, which was not a particularly sustainable approach.
- View CheatSheet-Python-6-Coding-Interview-Questions-Email-Course-Ad.pdf from CSE 110 at BRAC University. Python Cheat Sheet: 14 Interview Questions “ A puzzle a day to learn, code,.
- Python Interview Cheat Sheet Python Cheat Sheet can be used also for your Python Job interview. Before your technical interview, you can check this sheet and you can use it as Python Interview Cheat Sheet. You can find all the basic terms of Python programming languages in this cheat sheet.
Solve an example with your brain and hands and see what “algorithm” you used.
B.U.D.
Bottlenecks, Unnecessary work, Duplicated workWalk through your best algorithm and look for these flaws.
Space/Time tradeoffs
Can I save time by using more space ?
- Hash tables
- Frequency arrays
- Dynamic programming
Stack
- $O(1)$:
l = []
,l.append()
,l.pop()
,l[i]
,len(l)
- $O(n)$:
del l[i]
,l[inf:sup:step]
- $O(nlog_2(n))$:
l.sort()
,sorted(l)
- $O(1)$:
Queue
- $O(1)$:
dq = deque()
,dq.popleft()
,dq.appendleft(x)
+ normal list operations except slices
- $O(1)$:
Dictionary / Hashtable
- $O(1)$:
dic = {}
,key in dic
,dic[key]
,del dic[key]
on average, worst case $O(n)$
- $O(1)$:
Set
- $O(1)$:
s = set([])
,x in s
,s.add(x)
,del s[x]
- $O(n_1)$:
s1|s2
,s1&s2
,s1-s2
,s1^s2
- $O(1)$:
Heap / Priority Queue
- $O(1)$:
h = []
- $O(log_2(n))$:
heappush(h, x), heappop(h)
- $O(nlog_2(n) )$:
heap = heapfy([1, 5, 2, 3...])
left = 2*i; right = left+1; father = i//2
ifh[0]
in a 1-based arrayleft = 2*i+1; right = 2*i+2; father = depends on parity
otherwise
- $O(1)$:
Union find
- to be continued
Bisection search ends when left
and right
cross.
When will they cross ?
f(x, key) = x < key
➜ Moveright
whenx key
- Index of the first occurrence of key
f(x, key) = x <= key
➜ Moveleft
whenx key
- Index of the last occurrence of key + 1
- Might be necessary to check if the final value points to key
Merge sort
- Split the list in 2
- Recursively sort the left-most part and right-most part
- Merges these parts
Counting sort
- Count the occurences of each possible values
- Put the number of occurence times each value in the array, starting from the smallest one
Simple steps
- Recursive solution
- Store intermediate results
- Bottom up (Optional)
- Consider adding dummy values because things like
res[x][y]
may throw index errors on corner cases. - It can be easier to find the size of the best solution and then backtrack to find the solution.
Recursive solution
Memoization
@lru_cache(maxsize=None)
can automate the memoization process by using a dictionary to store the result of function calls (less efficient than an array).
Bottom up
Operation
x << y
Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y.x >> y
Returns x with the bits shifted to the right by y places. This is the same as //‘ing x by 2**y.x & y
Does a “bitwise and”. Each bit of the output is 1 if the corresponding bit of x AND of y is 1, otherwise it’s 0. Commutative and Associative.x | y
Does a “bitwise or”. Each bit of the output is 0 if the corresponding bit of x AND of y is 0, otherwise it’s 1. Commutative and Associative.~ x
Returns the complement of x - the number you get by switching each 1 for a 0 and each 0 for a 1. This is the same as -x - 1.x ^ y
Does a “bitwise exclusive or”. Each bit of the output is the same as the corresponding bit in x if that bit in y is 0, and it’s the complement of the bit in x if that bit in y is 1. Commutative and Associative.
Coding sets on bits
Python Cheat Sheet
Efficient when there is less than 64 possible values
Set | Binary |
---|---|
$emptyset$ | 0 |
${i}$ | 1 << i |
${0, 1, ldots, n-1}$ | (1 << n) - 1 |
$A cup B$ | `A |
$A cap B$ | A & B |
$(A setminus B) cup (B setminus A)$ | A ^ B |
$A subseteq B$ | A & B A |
$i in A$ | (1 << i) & A |
${min(A)}$ | -A & A |
You should ideally now about:
(Most are overkill for an interview but nice to know as a software engineer)
Numeric/mathematical
- Kadane’s algorithm (maximum subarray)
- every subarray has an ending
- the maximum subarray ending at the spot
i + 1 is either
- maximum subarray ending at the spot
i
+A[i + 1]
A[i + 1]
- maximum subarray ending at the spot
- Boyer-Moore majority vote algorithm
- find a candidate for the majority element
- keep a counter of the number of occurence of the current candidate while iterating through the list
++
if the current element is the candidate--
otherwise- if counter reaches 0, candidate = current candidate
- check if the candidate is the majority element
- find a candidate for the majority element
- Quickselect / Median-of-medians
- Reservoir sampling
- Sieve of Eratosthenes
- Alias method
- Euclid’s algorithm
- if
b != 1
return =gcd(b, a % b)
- else return a
- if
- Exponentiation by squaringMatrix exponentiation
- Just square exponentiation with matrix
res = res * a
if n odda = a * a
otherwise
- Matrix exponentiation
- use exponentiation by squaring
- Range minimum query
Tree/graph
- BFS
- stack or recursion
- DFS
- queue
- Dijkstra’s algorithm
- A* search
- Toposort
- Morris traversal
- Prim’s algorithm
- Kruskal’s algorithm
- Bellman-Ford algorithm
- Graham scan algorithm
- Ford-Fulkerson algorithm / Edmunds-Karp algorithm
- Floyd’s cycle finding algorithm
- Closest pair of points algorithm
- Hopcroft-Karp algorithm
Python Coding Questions Interview
String
- Longest common subsequence
lcs[p][q]
= lcs ending at indexp
in str1 andq
in str2 (DP)lcs[p][q]
=str1[p] str2[q] ? 1 + lcs[p-1][q-1] : max(lcs[p][q-1], lcs[p-1][q])
- Rabin-Karp algorithm
- Knuth-Morris-Pratt algorithm
- Aho-Corasick algorithm
- Ukkonen’s algorithm / SA-IS algorithm
- Manacher’s algorithm
Search
- Interpolation search
- Meta binary search
Sorting
- Heap sort
- Quicksort + Three way partitioning + Median of three
- Dutch national flag algorithm
Other
- Fisher-Yates shuffle
- Shunting-yard algorithm
- Minimax
- Mo’s algorithm
- Square root decomposition
- Rolling hash
Python Cheat Sheet Download
Questions about the stack, the tests, the integration process, documentation of production incidents…
Standard dev environment ? Mandatory ?
What are the weekly meetings ?
What’s the remote policy ?
What’s the product schedule and deployment frequency ?
What about conference/event attending ?
Is there any dedicated time for self-training ? Contributing to FOSS ?
What does it take to be successful here ?
To evaluate the global software engineering quality using the joel (StackOverflow co-founder) test:
- Do you use source control?
- Can you make a build in one step?
- Do you make daily builds?
- Do you have a bug database?
- Do you fix bugs before writing new code?
- Do you have an up-to-date schedule?
- Do you have a spec?
- Do programmers have quiet working conditions?
- Do you use the best tools money can buy?
- Do you have testers?
- Do new candidates write code during their interview?
- Do you do hallway usability testing?