Lesson 38/48 ยท ๐ฏ Problem Solving
๐ฏ Problem SolvingLesson 38/48
Phase 6 ยท Problem Solving35 min
How to Crack Any Coding Interview
A repeatable framework for tackling interview problems under pressure
The coding interview isn't just about knowing algorithms, it's about communicating your thinking. Here's the framework that top engineers use, every time.
The 5-step interview framework:
1. Clarify, Ask questions. What are the constraints? Can there be duplicates? Can input be empty?
2. Example, Draw a small example on paper. Input โ expected output. Walk through it manually.
3. Brute force, Describe the simplest solution out loud. Don't code yet.
4. Optimise, Think about whether you can do better. "Can I avoid the nested loop?"
5. Code, Now write the code. Clean, with variable names that make sense.
1. Clarify, Ask questions. What are the constraints? Can there be duplicates? Can input be empty?
2. Example, Draw a small example on paper. Input โ expected output. Walk through it manually.
3. Brute force, Describe the simplest solution out loud. Don't code yet.
4. Optimise, Think about whether you can do better. "Can I avoid the nested loop?"
5. Code, Now write the code. Clean, with variable names that make sense.
Talk out loud the entire time. Interviewers want to hear your thinking. Silence is the enemy. Even saying "I'm not sure yet, let me think about this step..." is better than silence.
Classic interview pattern: Sliding Windowpython
# Problem: Find the maximum sum of a subarray of size k
def max_subarray_sum(arr, k):
# Window: track the running sum of k elements
window_sum = sum(arr[:k]) # sum of first window
max_sum = window_sum
for i in range(k, len(arr)):
window_sum += arr[i] # add new element
window_sum -= arr[i - k] # remove old element
max_sum = max(max_sum, window_sum)
return max_sum
print(max_subarray_sum([2, 1, 5, 1, 3, 2], 3)) # 9 (5+1+3)Practice Exercises
0/2 solvedExercise 1 of 2medium
โฑ 00:00Valid Parentheses
Write a function
Rules:- Open brackets must be closed in the correct order-
Hint: Use a stack (list)! Push opening brackets, pop when you see a closing bracket.
is_valid(s) that checks if a string of brackets is valid.Rules:- Open brackets must be closed in the correct order-
"()" โ True- "()[]{}"" โ True- "(]" โ False- "([)]" โ FalseHint: Use a stack (list)! Push opening brackets, pop when you see a closing bracket.
solution.py
1 / 2
Exercise 2 of 2hard
โฑ 00:00Maximum Subarray (Kadane's Algorithm)
Write a function
Example:
This is a classic interview problem. The trick: keep a running sum, reset to 0 when it goes negative.
max_subarray(nums) that finds the contiguous subarray with the largest sum.Example:
max_subarray([-2,1,-3,4,-1,2,1,-5,4]) โ 6 (subarray [4,-1,2,1])This is a classic interview problem. The trick: keep a running sum, reset to 0 when it goes negative.
solution.py
2 / 2
Solve all 2 exercises to unlock completion