top of page

The five-step process to solving any LeetCode challenge problem

Many CodeConda students are at the stage of their learning where they are familiar with much of the syntax and structure of a certain language, but have difficulty pulling their knowledge together to solve a challenge problem. If this resonates with you, you've come to the right place! Fortunately, the ability to develop a solution to a coding problem is a skill that can certainly be learned. Further, there are five-steps any coder can follow to reach the solution to a problem more easily.



As an example, let's use LeetCode problem #9: "Palindrome Number". At the time of writing, LeetCode indicates only about 50% of attempts on this problem are successful.


1. Understand the question


Step 1 sounds simple, but you'd be surprised how many coders skip or half-ass this step. The first thing you need to do is fully understand what is being asked of you. LeetCode states the following:

Given an integer x, return true if x is a palindrome, and false otherwise.

Okay, so ask yourself if you understand the problem. If you don't know what a palindrome is, Google it. If you don't remember what an integer is, Google it. If you weren't paying attention when you first read the problem, read it again. Make sure you fully understand what they want here.


Basically, they're going to give us an integer (whole number, positive or negative), and they want us to determine whether it is the same forwards and backwards. Cool.


2. Study example inputs and outputs


Step 2 is really an extension of step 1. It's important to take a look at possible (acceptable) inputs to the problem, and what would be returned if everything works properly. This obviously also comes in handy when testing your solution. LeetCode makes this step easy by providing us with some examples:


Example 1:

Input: x = 121
Output: true

Example 2:

Input: x = -121
Output: false

Example 3:

Input: x = 10
Output: false

Reviewing inputs and outputs should also help you become familiar with edge cases and problem constraints. Truly grasp why each input produces each output. If you aren't sure about one example, don't move on until you figure it out.


3. Come up with instructions (an algorithm)


Unfortunately for us, we can't tell our program to "just look at it". It's pretty obvious to a human that the number 10 isn't the same forwards and backwards, but we need to tell the computer what steps it needs to take to definitely figure it out. This is also called an algorithm. There are usually many possible algorithms to solve a given problem, but all we need is one. You can write these instructions out, or just think them in your head. For this problem, the instructions could be:

Compare the character in the first position with the character in the last position. If they are different, return false. If they are the same, add one to the index of the first character and subtract one from the index of the last character. If the indexes are equal or the first index is now greater than the last index, return true. Repeat until one of the exit conditions are met.

4. Write pseudocode


Pseudocode is basically lines of text intended to represent code without any concern for proper syntax. Writing pseudocode allows you to quickly transform your planned instructions from step 3 into something that's closer to the real deal. Here is what the pseudocode for the "Palindrome Number" problem might look like:


left index = 0
right index = last index
while left index < right index
  if character at left index != character at right index
    return false
  else
    add 1 to left index
    subtract 1 from right index
return true

5. Convert to actual code


The nice thing about pseudocode is it's pretty easy to convert to any language. But for this example, let's say we wanted to use C++. All we'd have to do is go line-by-line, converting each to proper C++ syntax (and adding some extra lines of code where necessary). For example, it's not straightforward to "get at" each character in an integer, so we might want to first convert it to a string. This realization is something that will come with practice. Over time, as you work on challenge problems, you'll pick up more of these realizations earlier on. Here's what a C++ solution for "Palindrome Number" would look like:


#include <string>

using namespace std;

bool isPalindrome(int x) {
  string xStr = to_string(x);
  int leftIndex = 0;
  int rightIndex = xStr.length() - 1;
  while (leftIndex < rightIndex) {
    if (xStr[leftIndex] != xStr[rightIndex]) {
      return false;
    }
    else {
      leftIndex++;
      rightIndex--;
    }
  }
  return true;
}

And there you have it! You've solved the problem. Easy, right? Well, not quite. Even with these five useful steps, it'll will still take a lot of practice and repetition to get really good. If you're feeling discouraged or are at a roadblock in your coding journey, you should consider joining CodeConda.


CodeConda offers live, scheduled group lessons on various programming languages taught by experienced software engineers. During each lesson, you'll learn about a different key concept in your selected programming language, and walk through challenge problems just like "Palindrome Number". And your instructor will be available the entire time to answer any questions. Get started by clicking this link and scheduling your first lesson.

Comments


bottom of page