Which data structures and algorithms questions come up most often, and how should you prioritise them?
You look at which questions come up again and again, and you spend your practice time on those first.
from collections import Counter
recent = [
"two sum", "two sum", "valid parentheses", "two sum",
"reverse linked list", "valid parentheses", "binary search",
"merge intervals", "two sum", "binary search",
]
counts = Counter(recent)
for name, times in counts.most_common():
print(name, times)
And the output looks like this.
the question the times
------------------------ ---------
two sum 3
valid parentheses 2
binary search 2
reverse linked list 1
merge intervals 1
And the priority order is worth knowing about. Here is the list that covers most of the interviews, and it is roughly in the order of how often the questions come up.
the group the questions
------------------------ ------------------------------------------
the arrays and hashing two sum, group anagrams, longest substring
the two pointers container with most water, move zeroes
the sliding window longest substring without repeats
the linked lists reverse a list, detect a cycle, merge lists
the trees and graphs invert a tree, the lowest common ancestor
the dynamic programming climbing stairs, house robber, coin change
And there are two reasons to put a group first. The first one is how often it comes up. The second one is how many other questions it helps with. The two pointers and the hashing patterns together account for a huge number of the array questions, and once you know them the other array questions are much easier.
And there is a third reason to think about. The questions at the top of the list are the ones where the interviewer is checking the basics, so they are the ones where a mistake costs you the most. A clean answer on two sum is worth more than a clever answer on a rare problem.
So the rule to remember is this. Practise the frequent patterns first, and practise the simple versions of them properly.
The last question in this topic is about the system design questions that also involve data structures.