Many people feel discouraged when they struggle to solve even the "Easy" problems on Leetcode. But it's not their fault. There should be another level called "Pre-Easy" or "Foundational" that focuses on building the problem-solving muscle, guiding folks gradually into more advanced levels. In fact, I believe you can be 90% ready for your coding interview by working through the exercises below, without confusing yourself with advanced algorithms and data structures. If you strengthen your problem-solving skills with these foundational problems, learning the rest will become the easier part.
I’ve split the exercises into three categories: Array Indexing, Accumulator Variables and Recursion.
1. Array Indexing
Understanding how to navigate arrays is essential. Here are ten exercises, sorted in increasing difficulty, that build upon each other:
Iterate Over an Array
Write a function that prints each element in an array in order from the first to the last.
Iterate Over an Array in Reverse
Modify the previous function to print the elements in reverse order, from the last to the first.
Fetch Every Second Element
Write a function that accesses every other element in the array, starting from the first element.
Find the Index of a Target Element
Write a function that searches for a specific element in an array and returns its index. If the element is not found, return -1.
Find the First Prime Number in an Array
Iterate over an array and find the first prime number. Stop the iteration once you find it.
Traverse a Two-Dimensional Array
Write a function to print all elements of a 2D array (matrix), row by row.
Traverse the Main Diagonal of a Matrix
Print the elements along the main diagonal of a square matrix, where the row and column indices are equal.
Traverse the Perimeter of a Matrix
Print the elements along the outer edge (perimeter) of a 2D array.
Traverse Elements in Spiral Order
Print elements of a 2D array in spiral order, starting from the top-left corner and moving inward.
Traverse the Lower Triangle of a Matrix
Print the elements below and including the main diagonal of a square matrix.
2. Accumulator Variables
Learn how to keep track of values during iteration. These exercises build upon each other in complexity:
Calculate the Sum of an Array
Write a function that calculates the sum of all elements in an array by accumulating the total as you iterate.
Find the Minimum and Maximum Elements
Find the smallest and largest numbers in an array by updating minimum and maximum variables during iteration.
Find the Indices of the Min and Max Elements
In addition to finding the min and max values, keep track of their positions (indices) in the array.
Find the Two Smallest/Largest Elements Without Sorting
Modify your approach to keep track of the two smallest and two largest elements during a single pass through the array.
Count Occurrences of a Specific Element
Count how many times a given element appears in the array by incrementing a counter whenever you encounter it.
Count Occurrences of All Elements
Use a dictionary or map to count the number of times each unique element appears in the array during a single iteration.
Find the Two Most Frequent Elements
Find the two elements that appear the most number of times in an array.
Compute Prefix Sums
Create an array where each element at index
i
is the sum of all elements up to that index in the original array. We call this array prefix sums array.Find the Sum of Elements in a Given Range
Given a range (start and end indices), write a function that calculates the sum of elements within that range by iterating from the start to the end index and accumulating the sum.
Efficient Range Sum Queries Using Prefix Sums
After computing the prefix sums array, answer multiple range sum queries efficiently:
Instead of summing elements for each query, use the prefix sums array to compute the sum of elements between indices
i
andj
in constant time.Hint: The sum from index
i
toj
can be calculated asprefix_sum[j] - prefix_sum[i - 1]
. This method requires understanding how to manipulate indices and handle edge cases wheni
is0
.
3. Recursion
Get comfortable with functions that call themselves with these ten exercises:
Calculate the nth Fibonacci Number
Write a recursive function to find the nth Fibonacci number, where each number is the sum of the two preceding ones.
Sum of an Array Using Recursion
Find the sum of all elements in an array by recursively summing the first element and the sum of the rest of the array.
Find the Minimum Element in an Array Using Recursion
Find the smallest element in an array without using loops by comparing the first element with the minimum of the rest of the array.
Reverse a String Using Recursion
Reverse a given string by recursively swapping characters from the ends towards the center.
Check if a String is a Palindrome Using Recursion
Determine if a string reads the same backward as forward by comparing characters from the outside in.
Generate All Permutations of a String
Recursively generate all permutations of the characters in a string by swapping characters.
Generate All Subsets of a Set
Generate all possible subsets (the power set) of a set of numbers by including or excluding each element recursively.
Compute the Sum of Digits of a Number
Given an integer, write a recursive function to compute the sum of its digits.
Compute the Power of a Number
Write a recursive function to compute
x
raised to the powern
(i.e., computex^n
), wheren
is a non-negative integer.Count the Number of Occurrences of a Character in a String Using Recursion
Write a recursive function to count how many times a specific character appears in a string.
Wrapping Up
By working through these foundational exercises, you're not just preparing for coding interviews—you're building the core problem-solving muscles that will serve you throughout your programming journey. Remember, the goal is to understand the why and how, not just the what.
A Note on Testing
You'll notice that I haven't provided any test cases or solutions for these exercises. That's intentional. Writing your own tests and verifying your solutions is a critical skill—one that platforms like Leetcode don't always help you develop. By creating your own test cases, you learn to think deeply about edge cases, input validation, and potential bugs. This practice not only enhances your coding abilities but also mirrors real-world development, where testing is an integral part of the process.
1:1 Session With Me
I’m now taking requests for 1:1 sessions with me. I help folks by providing mock interviews (coding, system design, behavioral) with detailed feedback, interview preparation plans, and navigating tech career. You can book a session with me by clicking here.
I used Chat GPT to design Test cases for Array problems and got an idea of how I can think of those test cases.