CodeGround
DashboardProblemsInterviewsAssessmentsCourses
CodeGround
DashboardProblemsInterviewsAssessmentsCourses
The Technical Interview Course

0 of 170 lessons complete

  • Two pointers, walking inward
  • Letting the comparison steer you
  • Fix one, scan the rest
  • Proving a discard is safe
  • Carrying state on both sides
  • Retrieval: pointers against hashing

Two Pointers

21 / 170

Two Pointers/Lesson 6 of 6·About 48 min

Retrieval: pointers against hashing

Two problems rebuilt cold, plus the discrimination that decides which tool a new problem wants.

You now have two ways to find a pair with a property: a hash map from Unit 1, and converging pointers from this unit. They are not interchangeable, and picking wrong is the most common way this material fails to transfer.

Key idea

The deciding questions

Is the input sorted, or are you allowed to sort it? Two pointers need order. A hash map does not care.

Do you need the original positions? Sorting destroys them unless you carry them along. A hash map keeps them for free.

Is extra memory allowed? Two pointers are O(1) space. A hash map is O(n).

Sorted input with an O(1) space requirement means two pointers. Unsorted input where positions matter means a hash map. When both work, say so and pick one out loud.

Quick check· 5 min

Which tool fits?

An unsorted array of prices, and you must return the positions of two prices summing to a budget. The array can hold a million entries. Which approach fits best?

Problem· 20 min

3Sum, from scratch

You have solved this one before. Rebuild it from scratch without looking at your old submission. If the approach does not come back within a few minutes, that is the signal that it needs another pass.

Problem· 18 min

Longest Consecutive Sequence, from scratch

You have solved this one before. Rebuild it from scratch without looking at your old submission. If the approach does not come back within a few minutes, that is the signal that it needs another pass.

PreviousCarrying state on both sides
NextCarrying one value forward

0 of 4 parts complete