Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

JavaScript (ES2026) Module 7

Strings, Unicode and regular expressions interview questions

Interview questions on strings, Unicode and regular expressions: warm-ups, then what each harder question tests and how a good answer starts.

9 questions: 3 warm-ups and 6 with model answers · Levels: Fresher to senior

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher to mid

    Why does calling test() twice on a regex with the g flag return true and then false?

    Answered in the lesson Regular expressions in JavaScript

  2. Fresher to mid

    What does RegExp.escape do, and when do you need it?

    Answered in the lesson Modern regex features and RegExp.escape

  3. Fresher to mid

    Why does btoa('नमस्ते') throw, and how do you Base64-encode any text correctly?

    Answered in the lesson Text encoding, bytes and Base64

Questions with model answers

What each question tests, the first step of a good answer and the follow-ups to expect.

  1. Mid to senior

    A Node.js script reads a 700 MB access log with readFileSync(file, 'utf8') and collects every URL with text.match(/https?:\/\/\S+/g). It crashes before matching anything, and on a 300 MB log it uses gigabytes of memory. Explain both failures and rewrite the approach so that memory stays flat.

    What it tests
    Engine limits on string length, what match with the g flag allocates, lazy iteration with matchAll, reading a file line by line as a stream, and keeping regex state per line.
    First step
    Separate the two failures. V8 limits a string to 536,870,888 characters (buffer.constants.MAX_STRING_LENGTH), so a 700 MB file cannot become one string and readFileSync throws before any regex runs. The 300 MB log fits, but the whole text, the array of matches and every matched string sit in memory together.
    Follow-ups

    From the lesson Regular expressions in JavaScript

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.

    Opening the model answers with your pass…

    Model answers open with any MySmartCoPilot pass.

    • Pro pass · 1 week₹79$2.99
    • 30 days₹179$5.99
    • 1 year₹1,699$45

    One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

    It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  2. Fresher to mid

    An import feature splits each line of a CSV file with line.split(','), and a row such as Asha,"Pune, MH",42 comes out with four fields instead of three. A teammate proposes a cleverer regex for split. Explain the bug, whether a regex can fix it, and what you would ship.

    What it tests
    How CSV quotes fields, what splitting with a regex cannot do, the cost of a lookahead that scans to the end of the line, and choosing a parser over a pattern.
    First step
    Read the format first. RFC 4180 lets a field contain commas, double quotes and line breaks when the field is enclosed in double quotes, with a quote inside written as two quotes. split(',') ignores the quotes, so the comma inside "Pune, MH" starts a new field.
    Follow-ups

    From the lesson Regular expressions in JavaScript

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  3. Mid to senior

    A product search endpoint in Node.js builds new RegExp(query, 'i') from what users type and tests it against product names. One request keeps the server at 100 % CPU, and every other request times out. Explain what happened, how you would confirm it, and how you would redesign the search.

    What it tests
    Catastrophic backtracking, why one slow match blocks a whole Node.js process, escaping user input, input limits, and safe options when users really need patterns.
    First step
    Note two facts. The user controls the pattern, not only the text, so they can send an evil one such as (a+)+$ with a name that almost matches. And Node.js runs JavaScript, regex matching included, on one thread, so while that match backtracks no other request is served.
    Follow-ups

    From the lesson Modern regex features and RegExp.escape

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  4. Fresher to mid

    A sign-up form checks names with /^[a-zA-Z ]{2,40}$/ and rejects people who type their names in Devanagari or Tamil, or with accents, as in Zoë. Rewrite the check with Unicode-aware regex features, and say what a name check should and should not try to enforce.

    What it tests
    Unicode property escapes, combining marks, the u and v flags, normalisation, and judging which rules belong in a name check at all.
    First step
    List what fails and why. [a-zA-Z] is ASCII only, so é and every Devanagari or Tamil letter is rejected. \p{L} alone is not enough either: the vowel signs and the virama of these scripts are combining marks, \p{M}, so /^\p{L}+$/u still rejects नमस्ते.
    Follow-ups

    From the lesson Modern regex features and RegExp.escape

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  5. Mid to senior

    To greet the user, a web page decodes the payload of a JWT with JSON.parse(atob(token.split('.')[1])). It works in testing, but for some users it throws, and for others names such as José come out garbled. Explain both failures, write a correct decoder, and say what the page must never do with the result.

    What it tests
    base64url against standard Base64, padding, binary strings against UTF-8 bytes, and the difference between decoding a token and verifying it.
    First step
    Look at what the payload is. RFC 7515 encodes each part as base64url, with - and _ instead of + and / and no padding, of the UTF-8 bytes of the JSON. atob expects the standard alphabet and returns one character per byte, so it throws on - or _ and turns multi-byte UTF-8 characters into Latin-1 pairs.
    Follow-ups

    From the lesson Text encoding, bytes and Base64

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  6. Mid to senior

    A mobile web app uploads photos as Base64 strings inside a JSON body. The API gateway rejects bodies over 4 MB, photos of about 3 MB already fail, and users on low-end phones report that the page freezes. Explain the size and memory costs with numbers, and propose a better upload.

    What it tests
    The 4 to 3 size cost of Base64, the copies held in memory, the JSON envelope, and sending binary data with fetch and FormData instead.
    First step
    Do the arithmetic. Base64 writes 3 bytes as 4 characters, so a 3 MiB photo, 3,145,728 bytes, becomes 4,194,304 characters, already 4 MiB before the JSON keys and quotes are added. Then count the copies the page holds: the file's bytes, the Base64 string and the JSON string.
    Follow-ups

    From the lesson Text encoding, bytes and Base64

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes

All JavaScript (ES2026) interview questions · The JavaScript (ES2026) track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.