Skip to content
End of the book · 7 problems

Practice problems

These aren't interview brain-teasers. Each is a small, original check that you understood one idea from the book — sum an array, walk a linked list, recurse over a tree. Fill in the function and run it. Pick your language with the tabs; it applies to every problem on the page.

Everything compiles and runs in your browser on a public compiler service — no account, nothing installed.

01

Sum an Array

Return the sum of all integers in the array (an empty array sums to 0).

Arrays - a single linear pass over a contiguous sequence

02

Reverse a String

Return a new string with the characters of the input in reverse order.

Strings - a string is an indexable sequence of characters

03

Contains a Duplicate

Return true if any value appears more than once in the array, otherwise false.

Hash sets - O(1) membership testing to spot a repeat in one pass

04

Binary Search

Given an ascending-sorted array, return the index of target, or -1 if it is absent.

Searching & complexity - halve the range each step for O(log n) lookup

05

Length of a Linked List

Count the number of nodes in a singly linked list (an empty list has length 0).

Linked lists - follow next pointers until you reach the end

06

Sum of a Binary Tree

Return the sum of every node's value in a binary tree (an empty tree sums to 0).

Trees - recursion over value + left subtree + right subtree

07

Sort Ascending

Return a new array holding the same integers ordered from smallest to largest.

Sorting - produce a non-decreasing ordering of the elements