Interview OS
Back to Coding Prep

Coding

Strings

Frequency counts, two-pointer scans, and parsing.

Coding pattern

Overview

String problems are array problems with a twist: you reason over characters, so charset, case sensitivity, and length limits matter. Most solutions lean on a frequency map or a sliding window to find, count, or transform substrings without re-scanning.

How it works

Coding pattern
InputModelScanOutputString inASCII/UnicodeFreq / windowfreq[26]Two pointersl..rResult
ClientServiceEdgeData

Step by step, with examples

  1. 1

    String in

    • Note charset, case sensitivity, and length limits.
  2. 2

    Freq / window

    • Use a character frequency map or a sliding window.
  3. 3

    Two pointers

    • Expand and contract to test candidate substrings.
  4. 4

    Result

    • Return a length, boolean, or transformed string.
    • Example: Longest substring w/o repeat

When to reach for it

  • Anagrams / palindromes
  • Substring problems
  • Tokenizing input

Example problem

Check if a string is a palindrome ignoring case and non-alphanumerics.

Approach

  • Normalize the string
  • Walk inward from both ends comparing chars

Solution

function isPalindrome(s) {
  const t = s.toLowerCase().replace(/[^a-z0-9]/g, "");
  let i = 0, j = t.length - 1;
  while (i < j) if (t[i++] !== t[j--]) return false;
  return true;
}

Complexity

Time O(n), Space O(n) for the cleaned string.

Common pitfalls

  • Unicode edge cases
  • Mutating immutable strings in a loop

Where this content comes from

For full transparency, this content is curated and verified from these sources:

Curated company-tagged problem banksRecurring interview pattern librariesOppZen-authored drills & solutions