10 Must‑Know Hand‑Coding Interview Algorithms Every Developer Should Master
This article presents the ten most frequently asked hand‑coding interview problems—quick sort, binary search, climbing stairs, two‑sum, max drawdown, merging sorted arrays, maximum subarray, longest non‑repeating substring, permutations, and three‑sum—each with clear Python implementations, difficulty ratings, occurrence probabilities, and sample outputs to boost your interview success.
Want to ace coding interviews? Below are the ten highest‑frequency hand‑coding interview questions with complete Python solutions.
1. Quick Sort
Problem: Write a quick sort algorithm.
Difficulty: Medium.
Occurrence probability: ~50% – mastering quick sort is a must‑have skill.
def quick_sort(arr,start=0,end=None):
if end is None:
end = len(arr)-1
if end<=start:
return(arr)
i,j = start,end
ref = arr[start]
while j>i:
if arr[j]>= ref:
j = j - 1
else:
# swap three elements in one line
arr[i],arr[j],arr[i+1] = arr[j],arr[i+1],arr[i]
i = i + 1
quick_sort(arr,start=start,end = i-1)
quick_sort(arr,start=i+1,end = end)
return(arr)
print(quick_sort([1,1,3,3,2,2,6,6,6,5,5,7]))Output:
[1, 1, 2, 2, 2, 3, 5, 5, 6, 6, 6, 7]2. Binary Search
Problem: Write a binary search algorithm that returns the index of a target in a sorted array, or -1 if not found.
Difficulty: Easy.
Occurrence probability: ~30%.
def binary_search(arr,target):
start,end = 0,len(arr)-1
while True:
if end - start <=1:
if target == arr[start]:
return(start)
elif target == arr[end]:
return(end)
else:
return(-1)
mid = (start + end)//2
if arr[mid]>=target:
end = mid
else:
start = mid
print(binary_search([1,4,7,8,9,12],9))
print(binary_search([1,4,7,8,9,12],3))Output: 4 and
-13. Climbing Stairs
Problem: A staircase has 10 steps; you can climb 1 or 2 steps at a time. How many distinct ways are there to reach the top?
Difficulty: Easy.
Occurrence probability: ~20%.
def climb_stairs(n):
if n==1:
return 1
if n==2:
return 2
a,b = 1,2
i = 3
while i<=n:
a,b = b,a+b
i +=1
return b
print(climb_stairs(10))Output:
894. Two‑Sum
Problem: Find indices of two numbers in a list that add up to a target value.
Difficulty: Easy.
Occurrence probability: ~20%.
def sum_of_two(arr,target):
dic = {}
for i,x in enumerate(arr):
j = dic.get(target-x,-1)
if j != -1:
return((j,i))
else:
dic[x] = i
return([])
arr = [2,7,4,9]
target = 6
print(sum_of_two(arr,target))Output:
(0, 2)5. Max Drawdown
Problem: Given an array, find two numbers x and y (with x occurring before y) that maximize the difference x‑y.
Difficulty: Medium.
Occurrence probability: ~20%.
def max_drawdown(arr):
assert len(arr)>2, "len(arr) should > 2!"
x,y = arr[0:2]
xmax = x
maxdiff = x-y
for i in range(2,len(arr)):
if arr[i-1] > xmax:
xmax = arr[i-1]
if xmax - arr[i] > maxdiff:
maxdiff = xmax - arr[i]
x,y = xmax,arr[i]
print("x=",x,",y=",y)
return(maxdiff)
print(max_drawdown([3,7,2,6,4,1,9,8,5]))Output: x= 7 ,y= 1 and
66. Merge Two Sorted Arrays
Problem: Merge two ascending‑sorted arrays into a single sorted array.
Difficulty: Easy.
Occurrence probability: ~15%.
def merge_sorted_array(a,b):
c = []
i,j = 0,0
while True:
if i==len(a):
c.extend(b[j:])
return(c)
elif j==len(b):
c.extend(a[i:])
return(c)
else:
if a[i]<b[j]:
c.append(a[i])
i=i+1
else:
c.append(b[j])
j=j+1
print(merge_sorted_array([1,2,6,8],[2,4,7,10]))Output:
[1, 2, 2, 4, 6, 7, 8, 10]7. Maximum Subarray Sum
Problem: Find the maximum sum of any contiguous subarray.
Difficulty: Medium.
Occurrence probability: ~15%.
def max_sub_array(arr):
n = len(arr)
maxi,maxall = arr[0],arr[0]
for i in range(1,n):
maxi = max(arr[i],maxi + arr[i])
maxall = max(maxall,maxi)
return(maxall)
print(max_sub_array([1,5,-10,2,5,-3,2,6,-3,1]))Output:
128. Longest Non‑Repeating Substring
Problem: Given a string, find the longest substring without repeating characters.
Difficulty: Hard.
Occurrence probability: ~10%.
def longest_substr(s):
dic = {}
start,maxlen,substr = 0,0,""
for i,x in enumerate(s):
if x in dic:
start = max(dic[x]+1,start)
dic[x] = i
else:
dic[x] = i
if i-start+1>maxlen:
maxlen = i-start+1
substr = s[start:i+1]
return(substr)
print(longest_substr("abcbefgf"))
print(longest_substr("abcdef"))
print(longest_substr("abbcddefh"))Output: cbefg, abcdef,
defh9. Permutations
Problem: Generate all possible permutations of an array (handling duplicates).
Difficulty: Medium.
Occurrence probability: ~10%.
import numpy as np
def permutations(arr):
if len(arr)<=1:
return([arr])
t = [arr[0:1]]
i = 1
while i<=len(arr)-1:
a = arr[i]
t = [xs[0:j]+[a]+xs[j:] for xs in t for j in range(i+1)]
t = np.unique(t,axis=0).tolist()
i = i+1
return(t)
print(permutations([1,1,3]))Output:
[[1, 1, 3], [1, 3, 1], [3, 1, 1]]10. Three‑Sum
Problem: Find all unique triplets in an array that sum to a target value.
Difficulty: Hard.
Occurrence probability: ~5%.
def sum_of_three(arr,target):
assert len(arr)>=3, "len(arr) should >=3!"
arr.sort()
ans = set()
for k,c in enumerate(arr):
i,j = k+1,len(arr)-1
while i<j:
if arr[i]+arr[j]+c < target:
i = i+1
elif arr[i]+arr[j]+c > target:
j = j-1
else:
ans.update({(arr[k],arr[i],arr[j])})
i = i+1
j = j-1
return(list(ans))
print(sum_of_three([-3,-1,-2,1,2,3],0))Output:
[(-2, -1, 3), (-3, 1, 2)]Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
