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.

Regular Expressions Module 1

Foundations: what a regex is and how matching works interview questions

Interview questions on “Foundations: what a regex is and how matching works”: warm-ups, then what each harder question tests and how a good answer starts.

7 questions: 3 warm-ups and 4 with model answers · Levels: Fresher to senior

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher to mid

    How is a regular expression different from searching for a fixed word?

    Answered in the lesson What a regular expression is and where it runs

  2. Fresher to mid

    Why can the same pattern report a different match position in different languages?

    Answered in the lesson What a regular expression is and where it runs

  3. Fresher to mid

    Which questions do Python's search, match and fullmatch answer, and what do JavaScript's test and Go's MatchString answer?

    Answered in the lesson How an engine finds a match, step by step

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 teammate validates whole user-entered URLs with one long regular expression; broken URLs slip through and some valid ones are rejected. When is a regex the wrong tool for a job like this, and what would you use instead for a URL, for JSON and for HTML?

    What it tests
    Whether the candidate knows that nested, quoted or recursive formats need a parser rather than a flat pattern, and can name the right tool for each format.
    First step
    Separate what a regex can check from what it cannot. A pattern can test a small fixed shape, but a URL has structure — scheme, host, path, query, percent-encoding — that a flat pattern cannot track, so reach for the platform URL type, a JSON parser and an HTML parser, and keep regex for the small fields inside the parsed result.
    Follow-ups

    From the lesson What a regular expression is and where it runs

    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; nothing renews unless you choose to. 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. Mid to senior

    A highlighter finds a word with Go and gets its byte offset, then uses that number to cut the word out of a JavaScript string in the browser. On text with an accented letter or an emoji the highlight lands on the wrong characters. Explain the bug and fix it.

    What it tests
    Whether the candidate knows match positions are counted in different units — bytes, code points, code units — and keeps one engine's counting from end to end.
    First step
    Name the units. Go returns a byte offset, but a JavaScript string is indexed in UTF-16 code units, so the same number points at a different place once a character needs more than one byte or one unit. Recompute the offset in the target string instead of carrying Go's byte number across to JavaScript.
    Follow-ups

    From the lesson What a regular expression is and where it runs

    Model answer, Pro.

    In the answer:

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

    A syntax highlighter finds operators with the pattern <|<=|<<|<<=|=. On the text x <<= 2 it colours <, < and = as three tokens instead of one shift-assign token. Why does it do that, and how do you fix the pattern?

    What it tests
    Applying the leftmost-first rule to alternation, seeing that the order of alternatives is part of the meaning, and fixing it without breaking the other operators.
    First step
    Run the pattern by hand on the text at the position of the first <. List which alternatives can match there and in which order the engine tries them, then note which one it stops at.
    Follow-ups

    From the lesson How an engine finds a match, step by step

    Model answer, Pro.

    In the answer:

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

    A Python service and a nightly job that runs grep -E share the unit pattern m|mm|min. On the text 15 min the service extracts m and the job extracts min. Which is right, why do they differ, and how do you make them agree?

    What it tests
    Knowing that two match-selection rules exist, who uses which, and how to write or test patterns that do not depend on the rule.
    First step
    Write down the matches that start at the m of min, then ask which rule picks m and which picks min. Look up what each tool documents before you decide who is wrong.
    Follow-ups

    From the lesson How an engine finds a match, step by step

    Model answer, Pro.

    In the answer:

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

All Regular Expressions interview questions · The Regular Expressions 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.