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.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher to mid
Why does calling test() twice on a regex with the g flag return true and then false?
-
Fresher to mid
What does RegExp.escape do, and when do you need it?
Answered in the lesson Modern regex features and RegExp.escape
-
Fresher to mid
Why does btoa('नमस्ते') throw, and how do you Base64-encode any text correctly?
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
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
-
- What if the URLs were only counted, not collected?
- Would matchAll on the whole text fix the memory problem?
- What changes if a match could span two lines?
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.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
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
-
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
-
- Does a pattern that splits only on commas outside quotes solve it?
- What happens with a quoted field that contains a line break?
- When is a regex enough for CSV?
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
-
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
-
- The product team wants users to keep wildcards like *. How?
- Would a timeout around the call help?
- How would you find other risky patterns in the code base?
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
-
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
-
- Should the check allow digits or emoji?
- How do you count the 40-character limit?
- Where should the check run?
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
-
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
-
- Why does it work for most test users?
- Is it safe to show the user's role from the payload?
- How would you write it for browsers without fromBase64?
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
-
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
-
- Is gzip on the request a fix?
- When is Base64 in JSON acceptable?
- How do you upload a 200 MB video?
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