🐍 Python for Everyone

← Back to practice bank

Chapter 7 Coding Challenges 50-65 min, auto-graded

Coding Challenges: Lists & Tuples

8 HackerRank-style challenges, auto-graded right in your browser — no setup required. Write your function in the code box, then click Run Tests to check it against a set of test cases (including edge cases like empty lists). Stuck? Every challenge has a Show Solution reveal at the bottom. Only uses what Chapters 1-7 have covered so far — no dictionaries, no comprehensions, no def beyond the function stub already given.

How grading works Your code runs entirely in your browser using Pyodide (Python compiled to WebAssembly) — nothing is uploaded anywhere. Keep the function name in the stub exactly as given, since the grader calls it by name.

1 Reverse a List In Place Easy

Write a function reverse_in_place(lst) that reverses lst in place — without calling .reverse() or using slicing — by swapping elements from the outside in, and returns lst.

Constraints: lst may be empty, have one element, or have an even or odd number of elements.
Examples:
reverse_in_place([1, 2, 3]) → [3, 2, 1]
reverse_in_place([5]) → [5]
reverse_in_place([]) → []
challenge_1.py
Show Solution
solution_1.py
def reverse_in_place(lst):
    left = 0
    right = len(lst) - 1
    while left < right:
        lst[left], lst[right] = lst[right], lst[left]
        left = left + 1
        right = right - 1
    return lst

2 Find the Second-Largest Number Easy

Write a function second_largest(nums) that returns the second-largest distinct value in nums, without using sorted(). If every value in nums is the same, return None (there is no second-largest value).

Constraints: nums has at least one element; it may contain duplicates or negative numbers.
Examples:
second_largest([10, 20, 4, 45, 99]) → 45
second_largest([1, 2]) → 1
second_largest([3, 3, 3]) → None
challenge_2.py
Show Solution
solution_2.py
def second_largest(nums):
    largest = None
    second = None
    for n in nums:
        if largest is None or n > largest:
            second = largest
            largest = n
        elif n != largest and (second is None or n > second):
            second = n
    return second

3 Remove Duplicates, Keep Order Easy

Write a function remove_duplicates_preserve_order(lst) that returns a new list with duplicate values removed, keeping only each value's first appearance and preserving the original order.

Constraints: lst may be empty, contain no duplicates at all, or contain strings instead of numbers.
Examples:
remove_duplicates_preserve_order([1, 2, 2, 3, 1, 4]) → [1, 2, 3, 4]
remove_duplicates_preserve_order([1, 1, 1]) → [1]
remove_duplicates_preserve_order([]) → []
challenge_3.py
Show Solution
solution_3.py
def remove_duplicates_preserve_order(lst):
    result = []
    for value in lst:
        if value not in result:
            result.append(value)
    return result

4 Coordinate Distance Easy

Write a function coordinate_distance_squared(point_a, point_b) that takes two (x, y) coordinate pairs and returns the squared straight-line distance between them (skip the square root, since that hasn't been covered yet).

Constraints: coordinates may be zero, negative, or identical (distance 0).
Examples:
coordinate_distance_squared((0, 0), (3, 4)) → 25
coordinate_distance_squared((1, 1), (1, 1)) → 0
coordinate_distance_squared((-2, -3), (1, 1)) → 25
challenge_4.py
Show Solution
solution_4.py
def coordinate_distance_squared(point_a, point_b):
    x1, y1 = point_a
    x2, y2 = point_b
    return (x1 - x2) ** 2 + (y1 - y2) ** 2

5 Flatten a Nested List Medium

Write a function flatten_shallow(nested) that takes a list of lists (one level deep) and returns a single flat list containing every element, in order.

Constraints: nested may be empty, contain empty sublists, or have sublists of different lengths.
Examples:
flatten_shallow([[1, 2], [3, 4], [5]]) → [1, 2, 3, 4, 5]
flatten_shallow([[], [1], []]) → [1]
flatten_shallow([]) → []
challenge_5.py
Show Solution
solution_5.py
def flatten_shallow(nested):
    result = []
    for sub in nested:
        for item in sub:
            result.append(item)
    return result

6 Rotate a List Medium

Write a function rotate_list(lst, k) that returns a new list with lst rotated to the right by k positions (the last k elements move to the front). k may be larger than len(lst) — handle that by rotating by k % len(lst) instead.

Constraints: lst may be empty (return []); k may be 0 or greater than len(lst).
Examples:
rotate_list([1, 2, 3, 4, 5], 2) → [4, 5, 1, 2, 3]
rotate_list([1, 2, 3], 0) → [1, 2, 3]
rotate_list([1, 2, 3], 5) → [2, 3, 1]
challenge_6.py
Show Solution
solution_6.py
def rotate_list(lst, k):
    if len(lst) == 0:
        return []
    k = k % len(lst)
    if k == 0:
        return lst[:]
    return lst[-k:] + lst[:-k]

7 Merge Two Sorted Lists Medium

Write a function merge_sorted_lists(a, b) that merges two already-ascending-sorted lists into one ascending-sorted list, without calling sorted() or .sort() — walk both lists with two index pointers, always taking the smaller of the two current elements.

Constraints: either list may be empty; both lists are already sorted ascending and may contain duplicates.
Examples:
merge_sorted_lists([1, 3, 5], [2, 4, 6]) → [1, 2, 3, 4, 5, 6]
merge_sorted_lists([], [1, 2, 3]) → [1, 2, 3]
merge_sorted_lists([1, 1, 2], [1, 3]) → [1, 1, 1, 2, 3]
challenge_7.py
Show Solution
solution_7.py
def merge_sorted_lists(a, b):
    result = []
    i = 0
    j = 0
    while i < len(a) and j < len(b):
        if a[i] <= b[j]:
            result.append(a[i])
            i = i + 1
        else:
            result.append(b[j])
            j = j + 1
    while i < len(a):
        result.append(a[i])
        i = i + 1
    while j < len(b):
        result.append(b[j])
        j = j + 1
    return result

8 Find a Pair That Sums to a Target Medium

Write a function find_pair_sum(nums, target) that returns the first pair of values [nums[i], nums[j]] (with i < j, scanning i then j in order) whose sum equals target, or None if no such pair exists.

Constraints: nums may contain negative numbers or duplicates; if no pair sums to target, return None instead of crashing.
Examples:
find_pair_sum([2, 7, 11, 15], 9) → [2, 7]
find_pair_sum([1, 2, 3], 100) → None
find_pair_sum([-3, 4, 3, 90], 0) → [-3, 3]
challenge_8.py
Show Solution
solution_8.py
def find_pair_sum(nums, target):
    for i in range(len(nums)):
        for j in range(i + 1, len(nums)):
            if nums[i] + nums[j] == target:
                return [nums[i], nums[j]]
    return None
← Practice Bank Interview Questions →