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.
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.
lst may be empty, have one element, or have an even or odd number of elements.
reverse_in_place([1, 2, 3]) → [3, 2, 1]reverse_in_place([5]) → [5]reverse_in_place([]) → []
Show Solution
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).
nums has at least one element; it may contain duplicates or negative numbers.
second_largest([10, 20, 4, 45, 99]) → 45second_largest([1, 2]) → 1second_largest([3, 3, 3]) → None
Show Solution
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.
lst may be empty, contain no duplicates at all, or contain strings instead of numbers.
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([]) → []
Show Solution
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).
0).
coordinate_distance_squared((0, 0), (3, 4)) → 25coordinate_distance_squared((1, 1), (1, 1)) → 0coordinate_distance_squared((-2, -3), (1, 1)) → 25
Show Solution
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.
nested may be empty, contain empty sublists, or have sublists of different lengths.
flatten_shallow([[1, 2], [3, 4], [5]]) → [1, 2, 3, 4, 5]flatten_shallow([[], [1], []]) → [1]flatten_shallow([]) → []
Show Solution
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.
lst may be empty (return []); k may be 0 or greater than len(lst).
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]
Show Solution
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.
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]
Show Solution
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.
nums may contain negative numbers or duplicates; if no pair sums to target, return None instead of crashing.
find_pair_sum([2, 7, 11, 15], 9) → [2, 7]find_pair_sum([1, 2, 3], 100) → Nonefind_pair_sum([-3, 4, 3, 90], 0) → [-3, 3]
Show Solution
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