Absolutely! Here’s a list of 10 more Python problem-solving examples using your structured format:
✅ Format Reminder:
- 1. Problem Statement
- 2. Pseudocode
- 3. Flow/Decision Tree (Brief)
- 4. Input → Logic → Output Template
- 5. Why this Data Structure / Pattern
- 6. Final Code
🔷 1. Find First Unique Character in a String
Problem: Return the index of the first non-repeating character.
Pseudocode:
- Create counter for character frequency.
- Loop through string to find the first with count = 1.
Flow:
→ Count chars
→ Loop to find 1st non-repeating
Pattern: Hashmap + Single Pass
Final Code:
def solve(s):
from collections import Counter
count = Counter(s)
for i, c in enumerate(s):
if count[c] == 1:
return i
return -1
🔷 2. Two Sum (Find indices of 2 nums summing to target)
Pseudocode:
- Use hashmap to store nums seen so far.
- For each number, check if target - num is in map.
Flow:
→ Iterate → Lookup in map
Pattern: Hashing (O(n) lookup)
Final Code:
def solve(nums, target):
seen = {}
for i, num in enumerate(nums):
if target - num in seen:
return [seen[target - num], i]
seen[num] = i
🔷 3. Sliding Window Max Sum (Fixed Window)
Problem: Find max sum of any k
consecutive elements.
Pseudocode:
- Compute first k-sum.
- Slide the window: add next, subtract first.
- Track max.
Flow:
→ Window Slide → Track max
Pattern: Sliding Window (Fixed Size)
Final Code:
def solve(arr, k):
window_sum = sum(arr[:k])
max_sum = window_sum
for i in range(k, len(arr)):
window_sum += arr[i] - arr[i - k]
max_sum = max(max_sum, window_sum)
return max_sum
🔷 4. Recursive Fibonacci
Pseudocode:
- Base Case: if n == 0 or 1, return n
- Recursive Case: f(n) = f(n-1) + f(n-2)
Flow:
Recursive Tree
Pattern: Recursion
Final Code:
def fib(n):
if n <= 1:
return n
return fib(n-1) + fib(n-2)
🔷 5. Backtracking: Subsets of List
Pseudocode:
- At each step: include or not include element.
- Recurse to next index.
Flow:
Binary Tree → include/exclude
Pattern: Backtracking
Final Code:
def subsets(nums):
result = []
def backtrack(i, path):
if i == len(nums):
result.append(path[:])
return
path.append(nums[i])
backtrack(i+1, path)
path.pop()
backtrack(i+1, path)
backtrack(0, [])
return result
🔷 6. Minimum in Rotated Sorted Array
Pseudocode:
- Binary search: compare mid with right.
- Decide left/right half.
Flow:
→ Compare mid vs right
Pattern: Modified Binary Search
Final Code:
def find_min(nums):
l, r = 0, len(nums) - 1
while l < r:
m = (l + r) // 2
if nums[m] > nums[r]:
l = m + 1
else:
r = m
return nums[l]
🔷 7. Matrix Traversal – Spiral Order
Pseudocode:
- Traverse in 4 directions: Right → Down → Left → Up
- Shrink bounds each time
Flow:
→ Layer by layer
Pattern: Matrix Looping
Final Code:
def spiralOrder(matrix):
res = []
while matrix:
res += matrix.pop(0)
if matrix and matrix[0]:
for row in matrix:
res.append(row.pop())
if matrix:
res += matrix.pop()[::-1]
if matrix and matrix[0]:
for row in matrix[::-1]:
res.append(row.pop(0))
return res
🔷 8. Greedy – Jump Game
Pseudocode:
- Track maxReach as you go.
- If i > maxReach: can't reach → False
Flow:
→ Update maxReach
Pattern: Greedy
Final Code:
def canJump(nums):
maxReach = 0
for i, val in enumerate(nums):
if i > maxReach:
return False
maxReach = max(maxReach, i + val)
return True
🔷 9. Kadane’s Algorithm – Max Subarray Sum
Pseudocode:
- Initialize curr_sum and max_sum
- If curr_sum < 0, reset
Flow:
→ Local sum → global max
Pattern: Greedy + Sliding
Final Code:
def maxSubArray(nums):
curr_sum = max_sum = nums[0]
for num in nums[1:]:
curr_sum = max(num, curr_sum + num)
max_sum = max(max_sum, curr_sum)
return max_sum
🔷 10. Valid Parentheses
Pseudocode:
- Push open brackets on stack.
- Pop if matching close.
Flow:
→ Stack push/pop
Pattern: Stack
Final Code:
def isValid(s):
stack = []
mapping = {')':'(', ']':'[', '}':'{'}
for c in s:
if c in mapping.values():
stack.append(c)
elif c in mapping:
if not stack or stack[-1] != mapping[c]:
return False
stack.pop()
return not stack
Leave a Reply