Sitemap
The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +772K followers.

Member-only story

Problem Solving: Two Pointers approach

--

This is going to be my second article related to problem-solving. Here is my first one. I’m planning to write a few articles where I will discuss different problem-solving approaches that are important for coding interviews and also could be useful while tackling different kinds of issues in the software industry. This article will be about Two Pointers approach where I will talk about a few examples and use cases. So let’s get started.

Problem Statement #1

Given an array/list of integers nums and an integer target, we need to return indices of the two numbers such that they add up to the given target.

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

When I am presented with this problem statement and the example input, I would ask the following questions:

  1. I’m assuming there will be at least 2 numbers in the given array, right? Should I also take into consideration an edge case where the input array size might be less than 2? In that case which value(possibly some error code) should I return? Possibly MAX_INT or MIN_INT? or maybe -1?
  2. I’m assuming the numbers could be both -ve and +ve, right?
  3. Is the input array sorted? [This is a crucial question to ask because based on that we need to determine our solution approach].

We can ask a few more clarifying questions, however for this problem statement these should…

--

--

The Startup
The Startup

Published in The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +772K followers.

No responses yet