Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    DSADSA

    Foundations: measuring code

    • Big-O: how code grows
    • Space and the time–memory trade-off
    • Arrays and strings under the hood
    • Hash maps and sets
    • How to approach any interview problem

    Array patterns

    • Two pointers
    • Sliding window
    • Prefix sums
    • Binary search, done right
    • Sorting: merge sort and quicksort

    Stacks, queues and linked lists

    • Stacks and monotonic stacks
    • Queues and deques
    • Linked lists

    Recursion and trees

    • Recursion without fear
    • Backtracking: try and undo
    • Binary trees
    • Binary search trees
    • Heaps and priority queues

    Graphs and dynamic programming

    • Graphs: BFS and DFS
    • Shortest paths, topological sort and union-find
    • Dynamic programming: the idea
    • DP on grids and strings
    • Greedy: the best choice right now
    DSA

    Array patterns · Lesson 6 of 23

    Two pointers

    Two indexes moving through an array instead of nested loops: O(n²) becomes O(n).

    12 minutes3 games

    A simple idea that cracks many problems: instead of trying every pair (O(n²)), place two indexes and move them cleverly. Two common shapes:

    • From both ends inward: one at the start, one at the end, closing in (palindromes, pair sums in a sorted array, the biggest water container).
    • Fast and slow in the same direction: one reads, one writes (removing duplicates, moving zeros).
    JavaScript
    // Sorted array: find two numbers adding up to target
    function pairInSorted(nums, target) {
      let left = 0, right = nums.length - 1
      while (left < right) {
        const sum = nums[left] + nums[right]
        if (sum === target) return [left, right]
        if (sum < target) left++      // need a bigger sum: move the small side up
        else right--                  // need a smaller sum: move the big side down
      }
      return []
    }
    
    console.log(pairInSorted([1, 3, 4, 6, 9, 11], 13))
    console.log(pairInSorted([1, 2, 3], 100))
    Step by step1 / 5
    1. 1const nums = [1, 3, 4, 6, 9, 11]
    2. ▶let l = 0, r = 5
    3. 3// 1 + 11 = 12 < 13
    4. 4l++
    5. 5// 3 + 11 = 14 > 13
    6. 6r--
    7. 7// 3 + 9 = 12 < 13
    8. 8l++
    9. 9console.log(nums[l] + nums[r])

    Variables

    l0 → 1
    r5 → 11

    Output

    …

    1 + 11 = 12 is below 13. Anything paired with 1 is smaller still, so 1 is useless.

    Every step rules one element out for good, so there are at most n steps.

    Reader and writer

    JavaScript
    // Remove duplicates from a sorted array in place; return the new length
    function dedupe(nums) {
      let write = 1
      for (let read = 1; read < nums.length; read++) {
        if (nums[read] !== nums[write - 1]) nums[write++] = nums[read]
      }
      return write
    }
    
    const arr = [1, 1, 2, 3, 3, 3, 4]
    const n = dedupe(arr)
    console.log(n, arr.slice(0, n))

    Important

    The inward pattern needs a reason why the pointer you move can never help again. For pair sums that reason is the sort order. On an unsorted array, either sort first (O(n log n)) or use a hash map (O(n) memory).

    Quick question

    In "container with most water" (walls of given heights; water between two walls = the shorter one × the distance), the pointers are at the ends. Which do you move?

    Complete the code

    Complete the two-pointer palindrome check.

    function isPal(s) {
      let i = 0, j = s.length - 1
      while (i < j) {
        if (s[i] !== s[j]) return 
        i++
        --
      }
      return true
    }

    Interview question

    How do you solve 3Sum (three numbers summing to zero) faster than O(n³)?

    Exercise

    Write squaresSorted(nums): the array is sorted (with negatives); return the squares sorted ascending in O(n), without sort. It must print [0, 1, 9, 16, 100].

    Expected output: [0, 1, 9, 16, 100]

    JavaScript
    function squaresSorted(nums) {
      
    }
    
    console.log(squaresSorted([-4, -1, 0, 3, 10]))

    Practise on real problems

    Practice problems that use what you learned here. Solve them in JavaScript, Python or C++.

    • Pair in a sorted listEasy
    • Palindrome after cleaningEasy
    • Three numbers summing to zeroMedium
    • Largest water containerMedium
    • Zeros to the endEasy
    • Palindrome after one deletionEasy
    • Trapped rainwaterHard

    Learn more from

    • Wikipedia: 3SUMen.wikipedia.org
    • Tech Interview Handbook: Array cheatsheettechinterviewhandbook.org
    • MDN: Array.prototype.sort()developer.mozilla.org

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousHow to approach any interview problemNextSliding window
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.