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

    Foundations: measuring code · Lesson 3 of 23

    Arrays and strings under the hood

    What's fast and slow on arrays, and why building a string in a loop can be costly.

    10 minutes3 games

    An array keeps its elements next to each other in memory. Knowing the index, the machine computes the address directly: O(1). Inserting at the front or in the middle means shifting everything after it one place: O(n).

    nums =

    7[0]
    3[1]
    9[2]
    4[3]
    1[4]

    nums[2] jumps straight to 9, but nums.unshift(0) must shift all five.

    OperationComplexity

    arr[i], arr[i] = x

    O(1)

    push, pop

    O(1) amortized

    unshift, shift, splice(i, …)

    O(n)

    includes, indexOf, find

    O(n)

    slice, concat, [...arr]

    O(n) (a new copy)

    sort

    O(n log n)

    Strings are immutable

    JavaScript strings are immutable: s[0] = "x" does nothing, and every + makes a new string. When building a large result piece by piece, collect the parts in an array and join once at the end.

    To modify characters (say, reverse), turn the string into an array with [...s] and work on that.

    JavaScript
    // Reverse an array in place: two pointers swapping from both ends
    function reverseInPlace(arr) {
      let left = 0, right = arr.length - 1
      while (left < right) {
        [arr[left], arr[right]] = [arr[right], arr[left]]
        left++
        right--
      }
      return arr
    }
    
    console.log(reverseInPlace([1, 2, 3, 4, 5]))
    console.log(reverseInPlace([..."stressed"]).join(""))
    Step by step1 / 6
    1. ▶const a = [1, 2, 3, 4]
    2. 2let l = 0, r = 3
    3. 3;[a[l], a[r]] = [a[r], a[l]]
    4. 4l++; r--
    5. 5;[a[l], a[r]] = [a[r], a[l]]
    6. 6console.log(a)

    Variables

    a[1, 2, 3, 4]

    Output

    …

    Important

    In place means modifying the input itself without a copy, so the extra memory is O(1). It saves memory but changes the caller's data, so make it obvious in the name or docs (like JavaScript's sort and reverse, which mutate the original array).

    What's the output?

    const nums = [3, 1, 2]
    const sorted = nums.sort()
    sorted.push(9)
    console.log(nums.length)

    What will this code print?

    Complete the code

    Complete the function that moves every zero to the end in place, keeping the others in order.

    function moveZeros(nums) {
      let write = 0
      for (let read = 0; read < nums.length; read++) {
        if (nums[read] !== 0) {
          [nums[write], nums[read]] = [nums[read], nums[]]
          ++
        }
      }
      return nums
    }

    Interview question

    Why can str += ch inside a loop be O(n²), and what's the alternative?

    Exercise

    Write isPalindrome(s) with two pointers from both ends (without reversing the string). It must print true false.

    Expected output: true false

    JavaScript
    function isPalindrome(s) {
      
    }
    
    console.log(isPalindrome("racecar"), isPalindrome("seacode"))

    Practise on real problems

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

    • Zeros to the endEasy
    • Reverse the wordsMedium
    • Palindrome after cleaningEasy
    • Plus oneEasy
    • Rotate a matrix 90°Medium

    Learn more from

    • MDN: Arraydeveloper.mozilla.org
    • Wikipedia: Array (data structure)en.wikipedia.org
    • VisuAlgo: Arrayvisualgo.net

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousSpace and the time–memory trade-offNextHash maps and sets
    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.