Skip to main content

Command Palette

Search for a command to run...

String Polyfills and Common Interview Methods in JavaScript

Updated
4 min read
String Polyfills and Common Interview Methods in JavaScript

Most developers use string methods like includes, slice, or replace daily. They work, so nobody questions them. That’s fine, until you hit an interview or a bug where you actually need to understand what’s happening underneath.

We’ll break one method down, rebuild it ourselves, and then connect that same thinking to real interview problems.


String Methods Are Just Functions

When you write:

"hello".includes("he");

You’re calling a function that lives on String.prototype.

That means:

  • It takes input

  • It runs logic

  • It returns output

The only difference is, you don’t see the logic. Let’s fix that.

Why Developers Write Polyfills

People often say polyfills exist for old browsers. That’s true, but it’s not the real value for you.

The real reason you should care:

  • You understand how built-ins actually work

  • You stop guessing in interviews

  • You write better logic in general

If you can rebuild a method, you actually understand it.

How includes Actually Works

At a basic level, includes checks:

“Does this smaller string exist inside the bigger string?”

But how?

Step by step:

  1. Start from index 0

  2. Try matching the target string from that position

  3. If it matches fully, return true

  4. If not, move one step forward

  5. Repeat until the string ends

Writing Our Own includes (Polyfill)

String.prototype.myIncludes = function(search, start = 0) {
  if (start < 0) start = 0;

  for (let i = start; i <= this.length - search.length; i++) {
    let found = true;

    for (let j = 0; j < search.length; j++) {
      if (this[i + j] !== search[j]) {
        found = false;
        break;
      }
    }

    if (found) return true;
  }

  return false;
};

Let’s break this down properly.

Outer Loop (i)

This controls where we start matching in the main string.

Inner Loop (j)

This checks if every character of the search string matches.

Key Insight

The moment one character doesn’t match, we stop and move forward. No need to continue.

Some more pollyfills

1)Count Occurrences

function countOccurrences(str, char) {
  let count = 0;

  for (let i = 0; i < str.length; i++) {
    if (str[i] === char) count++;
  }

  return count;
}

2) Longest Substring Without Repeating Characters

function longestSubstring(str) {
  let set = new Set();
  let left = 0;
  let maxLength = 0;

  for (let right = 0; right < str.length; right++) {
    while (set.has(str[right])) {
      set.delete(str[left]);
      left++;
    }

    set.add(str[right]);
    maxLength = Math.max(maxLength, right - left + 1);
  }

  return maxLength;
}

Common Interview String Problems

Interviewers often move from polyfills to algorithmic string problems. You are usually explicitly asked not to use built-in methods like .reverse() or .split().

  • Reverse a String:

    • The Trap: str.split('').reverse().join('') is easy, but it creates a new array in memory ($O(N)$ space).

    • The Solution: Iterate backward through the string using a for loop and concatenate characters to a new string.

  • Valid Palindrome Check:

    • The Logic: Use a "Two-Pointer" approach. Place one pointer at the start (index 0) and one at the end (length - 1). Check if they match, then move them toward the center. This is highly efficient ($O(N)$ time, $O(1)$ space).
  • Valid Anagrams:

    • The Logic: Create a "frequency map" (an object/hash map) that counts the occurrences of each character in the first string. Iterate through the second string and subtract the counts. If all counts hit zero, it's an anagram.

Final Thought

Built-in methods are just hidden loops and conditions.

Once you see that, the gap between “using JavaScript” and “understanding JavaScript” disappears.

JS Under the Hood: The Engine Room

Part 17 of 24

Ever wondered why your code actually runs? We’re going beyond the syntax to explore the V8 engine, memory management, and the "magic" that happens between your keyboard and the screen. Just deep dives.

Up next

Understanding the this Keyword in JavaScript

The simplest way to think about this is to ask: "Who is calling me right now?" Here is a breakdown of how this behaves in different scenarios, keeping the focus strictly on the caller. 1. this inside