Array Exercises: Practice Problems (with Solutions)
Time to practice. Solve each problem yourself first — write the code, trace a small example by hand, and reason about its time and space complexity. Only then open the answer sheet. Every View answer link jumps to a full, multi-language solution on the array solutions page.
How to practice: Set a timer (15–20 min per problem). If you’re stuck, re-read the arrays introduction before peeking. Getting stuck and recovering is how the patterns stick.
Warm-ups
Q1. Reverse an array in place. Given an array, reverse the order of its elements without allocating a second array. Target O(n) time and O(1) extra space.
Q2. Find the maximum element. Return the largest value in an array of integers in a single pass. What should you return for an empty array?
Q3. Sum of all elements. Compute the sum of an integer array. State the time and space complexity.
Core problems
Q4. Two Sum. Given an array nums and a target, return the indices of the two numbers that add up to the target. Can you do better than the obvious O(n²)?
Q5. Move zeros to the end. Move all 0s to the end of the array while keeping the relative order of the non-zero elements, in place.
Challenge
Q6. Maximum subarray sum (Kadane’s algorithm). Find the largest sum of any contiguous subarray. Aim for a single O(n) pass.
Done? Review every solution on the array answer sheet — even the ones you solved, since there’s often a cleaner approach. Then continue to linked lists.