Before Leetcode - 30 Problems to Build Your Coding Muscle

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.

About me: I'm an ex-FAANG Senior Software Engineer currently on sabbatical. You can get daily coding interview tips from me straight to your email by subscribing to my newsletter called Faangshui here: blog.faangshui.com. Let's also connect on Linkedin! Now let's get back to the problems...

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:

  1. Iterate Over an Array: Write a function that prints each element in an array in order from the first to the last.
  2. Iterate Over an Array in Reverse: Modify the previous function to print the elements in reverse order, from the last to the first.
  3. Fetch Every Second Element: Write a function that accesses every other element in the array, starting from the first element.
  4. 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.
  5. 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.
  6. Traverse a Two-Dimensional Array: Write a function to print all elements of a 2D array (matrix), row by row.
  7. 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.
  8. Traverse the Perimeter of a Matrix: Print the elements along the outer edge (perimeter) of a 2D array.
  9. Traverse Elements in Spiral Order: Print elements of a 2D array in spiral order, starting from the top-left corner and moving inward.
  10. 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:

  1. 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.
  2. Find the Minimum and Maximum Elements: Find the smallest and largest numbers in an array by updating minimum and maximum variables during iteration.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. Find the Two Most Frequent Elements: Find the two elements that appear the most number of times in an array.
  8. 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.
  9. 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.
  10. 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 and j in constant time.
    • Hint: The sum from index i to j can be calculated as prefix_sum[j] - prefix_sum[i - 1]. This method requires understanding how to manipulate indices and handle edge cases when i is 0.

Okay, this post is getting too long. You can get the rest of the problems here: https://blog.faangshui.com/i/149072585/recursion

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.