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.
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
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
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
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
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
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
Sort Ascending
Return a new array holding the same integers ordered from smallest to largest.
Sorting - produce a non-decreasing ordering of the elements