Algorithm Practice Set — Level Up
15 Problems · 5 Easy · 5 Medium · 5 Hard
Name: ________________________________ Date: ______________
🟢 Easy: Problems 1–5 🟡 Medium: Problems 6–10 🔴 Hard: Problems 11–15
💡 Problem-Solving Checklist (use for every question):
☐ Understand the problem ☐ Work an example by hand ☐ Write steps in English ☐ Code ☐ Test
edge cases
EASY — Problems 1–5
1. What Does This Print? Easy | TRACE THE
CODE
Read the code below carefully. Without running it, trace through it step by step and write the exact
output.
Code:
x = 10
for i in range(1, 5):
if i % 2 == 0:
x = x - i
else:
x = x + i
print(x)
Fill in the trace table:
i i % 2 == 0? x (after update) printed
1
Algorithm Practice Set — Level Up
2. Count Characters in a Word Easy | WRITE THE
ALGORITHM
Given a word and a character, count how many times the character appears in the word.
Do not use the built-in .count() method — implement it yourself with a loop.
Examples:
Input Output
word = "banana", ch = 'a' 3
word = "hello", ch = 'z' 0
word = "mississippi", ch = 's' 4
Write your algorithm in plain English:
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Now write the Python code:
def solution(...):
# your code here
3. Sum of a List — Spot the Mistake Easy | FIX THE BUG
A student wrote this code to sum all elements in a list. It has one bug. Find and fix it.
Buggy code (line highlighted in red):
1 def list_sum(numbers):
2 total = 1
3 for n in numbers:
4 total = total + n
5 return total
Algorithm Practice Set — Level Up
What is wrong? (explain in one sentence)
Bug explanation: ___________________________________________
Write the corrected line below:
total = ___
4. String Slicer Easy | PREDICT THE
OUTPUT
Python string slicing uses s[start:stop:step]. Without running the code, write what each expression
evaluates to.
Expression (s = "algorithm") Your Answer
s[0:4]
s[-3:]
s[::2]
s[::-1]
s[2:7:2]
5. Complete the Function Easy | FILL IN THE
BLANK
The function below should return True if a number is a multiple of both 3 and 5, and False otherwise.
Fill in the blanks.
Code:
def is_multiple_3_and_5(n):
if n % ___ == 0 and n % ___ == 0:
return ___
return ___
Test your logic with these cases:
Input Output
Algorithm Practice Set — Level Up
n = 15 True
n = 9 False
n = 30 True
n = 7 False
MEDIUM — Problems 6–10
6. Zigzag Sum Medium | WRITE FROM
SCRATCH
Given a list of numbers, compute the zigzag sum: subtract the first element, add the second, subtract
the third, add the fourth, and so on.
In other words: result = -a[0] + a[1] - a[2] + a[3] - ...
Examples:
Input Output
[4, 7, 2, 8, 1] -4 + 7 - 2 + 8 - 1 = 8
[10, 3] -10 + 3 = -7
[5] -5
Write your algorithm in plain English:
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Now write the Python code:
def solution(...):
# your code here
Algorithm Practice Set — Level Up
7. Binary Search — Trace and Adapt Medium | TRACE +
MODIFY
Here is a binary search function. First, trace its execution. Then answer the modification question
below.
Code:
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
Trace for arr = [2, 5, 8, 12, 16, 23, 38, 56], target = 23:
low high mid arr[mid] action
0 7
Modification challenge:
Change the function so it returns the count of steps taken instead of the index. What is the count for
the trace above?
# Write your modified function here
8. Reverse Words — Three Bugs Medium | DEBUG +
EXPLAIN
This function should reverse the words in a sentence (not the letters). It has three bugs. Find all three.
Example: "hello world" → "world hello"
Code:
def reverse_words(sentence):
words = [Link](" ")
Algorithm Practice Set — Level Up
reversed_words = []
i = len(words)
while i > 0:
reversed_words.append(words[i])
i = i - 1
return " ".join(reversed_words)
List each bug, the line number, and the fix:
# Line What is wrong Fix
1
9. Rotate a List Medium | DESIGN THE
ALGORITHM
Given a list and an integer k, rotate the list to the right by k positions.
Do not use slicing shortcuts in your solution — use loops only.
Examples:
Input Output
[1, 2, 3, 4, 5], k=2 [4, 5, 1, 2, 3]
[7, 8, 9], k=4 [9, 7, 8]
[1, 2, 3], k=0 [1, 2, 3]
Write your algorithm in plain English:
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Now write the Python code:
def solution(...):
# your code here
Algorithm Practice Set — Level Up
Bonus: what happens if k > len(list)? How do you handle it?
10. Fibonacci: Iteration → Recursion Medium | CONVERT
THE APPROACH
Below is an iterative Fibonacci function. Your job is to rewrite it as a recursive function that produces
the same results.
Code:
def fib_iter(n):
if n == 0: return 0
if n == 1: return 1
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
Write the recursive version:
def fib_rec(n):
# your code here
Verify both give the same output:
Input Output
n = 0 0
n = 1 1
n = 6 8
n = 10 55
Algorithm Practice Set — Level Up
HARD — Problems 11–15
11. Find Duplicates — Two Approaches Hard | ANALYZE +
OPTIMIZE
Write two different solutions that find all duplicate elements in a list (elements that appear more than
once).
Approach A: use only loops and comparisons — no sets, no dicts.
Approach B: use a dictionary to count frequencies.
Examples:
Input Output
[1, 3, 4, 2, 2, 3, 5, 1] [2, 3, 1] (any order)
[7, 7, 7] [7]
[1, 2, 3] []
Compare the two approaches:
Approach A (loops) Approach B (dict)
Time complexity
Space complexity
Which is better for
large lists?
12. Stack-Based Bracket Checker Hard | DESIGN +
TRACE
Write a function that checks if a string of brackets is balanced. Use a stack (list) to track opening
brackets.
Balanced means every opening bracket ( [ { has a matching closing bracket ) ] } in the correct
order.
Examples:
Input Output
"({[]})" True
"{[}]" False
"((())" False
"" True
Algorithm Practice Set — Level Up
Trace your algorithm on "({[]})":
character action stack after
(
13. Student Grade Report Hard | REAL-WORLD
PROBLEM
You have a dictionary of students and their list of scores. Write a function that returns a summary
report as a dictionary.
The report must contain for each student:
• [object Object] — rounded to 1 decimal
• [object Object] — the best score
• [object Object] — the worst score
• [object Object] — 'A' (≥90), 'B' (≥75), 'C' (≥60), 'F' (below 60)
Input:
students = {
"Ali": [88, 92, 76, 95],
"Barno": [55, 60, 48, 70],
"Jasur": [100, 98, 95, 99],
}
Expected output (partially shown):
{"Ali": {"average": 87.8, "highest": 95,
"lowest": 76, "grade": "B"}, ...}
14. Is This Sort Correct? Analyze and Fix Hard | COMPLETE THE
PROOF
Algorithm Practice Set — Level Up
A student claims this is a working selection sort. Analyze the code: does it always work? If not, find
the bug and fix it. Then answer the questions below.
Code:
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i, n):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr
Trace on [5, 2, 8, 1, 9]:
i min_idx (after inner loop) array after swap
0
Question Your Answer
Is the inner loop range correct?
Why?
What is the time complexity?
Is this stable sort? (explain)
15. Caesar Cipher: Encode & Decode Hard | BUILD STEP BY
STEP
A Caesar cipher shifts each letter by k positions in the alphabet (wrapping around). Implement both
encode and decode functions.
Rules:
• Only letters are shifted; spaces and punctuation stay unchanged
• Preserve uppercase and lowercase
Algorithm Practice Set — Level Up
• Wrap around: 'z' shifted by 3 becomes 'c'
Examples:
Input Output
encode("Hello, World!", 3) "Khoor, Zruog!"
decode("Khoor, Zruog!", 3) "Hello, World!"
encode("xyz", 2) "zab"
After solving, answer this:
Can you write one single function that does both encode and decode using the same logic? How?
Algorithm Practice Set — Level Up