Using PREP to Solve Two Sum Challenge

Sarah Al-Said
Strategio
Published in
3 min readDec 22, 2022

Two Sum is a classic LeetCode challenge that is infamous for popping up in technical interviews. This article will walk you through the PREP method of how to solve this challenge for your next interview.

The Problem

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

You can also find the direct link here.

What is the PREP method?

Have you ever started coding a problem, only to realize halfway that you read it incorrectly? PREP is a mnemonic that is used for problem-solving coding challenges. It walks you through four imperative steps to gather all the information you need regarding the problem before you begin solving it. By following these steps and asking inquisitive questions to your interviewer, you will pass all the test cases.

  1. “P” for Parameters
  2. “R” for Returns
  3. “E” for Examples
  4. “P” for Pseudocode

As we solve the Two Sum challenge, we will approach it in these steps.

Parameters

In the first part, we will focus on what parameters our function will take in. The keywords that we would be looking for are “given,” “accepts,” or “takes in.” The questions you want to keep in mind are:

  • What parameters will the function accept?
  • What is the type for each parameter?
  • What meaningful names can be used for these parameters (if they haven't been provided)?

We know from the problem that we are “given an array of integers nums and an integer target,” these will be our two parameters. This will help us set up the function as shown below:

class Solution {
public int[] twoSum(int[] nums, int target) {

}
}

Returns

Next, we will ask ourselves what data type the problem asks us to return. Is it a number, boolean, string, etc.? The challenge gives us the following information: indices of the two numbers such that they add up to the target, each input would have exactly one solution, and you may not use the same element twice. With that information we know we will return the positions of two numbers, rather than the number themselves.

Examples

Now we will be looking at the examples that are provided. In case examples aren’t given, I would create my own starting from the simplest to the most complex, aiming at 3–5 test cases.

Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1]

Example 2: Input: nums = [3,2,4], target = 6 Output: [1,2]

Example 3: Input: nums = [3,3], target = 6 Output: [0,1]

Pseudocode

Once we know the parameters, returns, and examples of our function, we will write up our pseudocode.

What is pseudocode? Pseudocode is a series of statements written in spoken language comments describing what you need to do. This will give us an idea of what technique to approach the code, whether it is a hashmap, swapping numbers, using tules, etc. Our pseudocode should be precise enough that you and the interviewer know how the challenge will be solved. I will be using the brute force method to solve the Two Sum challenge so my pseudocode will look as follows:

//create the outer loop, setting i to run from i=0 to i=n-2
//create the inner for loop, setting j=i+1
//create if conditional in inner loop
//in each iteration, check if any indices nums[i] + nums[j] equal target
//if indices equal target return {outerIndex, innerIndex}
//if indices do not match, continue iteration to check the next pair

Solution

Great! Now you have all the tools to build your code. I will use the Java language to convert my pseudocode into real-time code.

class TwoSum {

// Time complexity: O(n^2)
private static int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
return new int[] { i, j };
}
}
}
return new int[] {};
}

You have completed the Two Sum challenge, and all our test cases have passed. I hope this PREP method will help you strategically approach the next coding challenge!

--

--