Interview replay

Full round replay — String vs StringBuilder

10 minjunior06 yrs

Timed verbal replay with pass/fail criteria per follow-up.

How to run this

Answer out loud, timed. Do not read the entry first. Then compare against "The Answer" and "Interviewer's Next Move" and mark yourself.

The opener

What is the difference between String, StringBuilder and StringBuffer?

Budget: 45 seconds. Going long here is itself a fail signal.

Follow-ups

  1. 1. “Why is concatenating in a loop slow?

    Testing: Mechanism, not the word 'immutable' on its own.

    Scoring

    Pass: String cannot be extended, so each += allocates a new String holding everything so far plus the addition and copies it. That is O(n^2) across the loop. Measured at 30,000 appends it is roughly 40x slower than a builder, and the gap widens with n.

    Fail: 'Because String is immutable' with no account of the copying.

  2. 2. “Does + compile to StringBuilder?

    Testing: JEP 280 — most answers are eight years out of date.

    Scoring

    Pass: Not since Java 9. It compiles to invokedynamic makeConcatWithConstants and the JVM sizes and fills the result in one pass. javac did emit StringBuilder chains up to Java 8.

    Fail: Yes, javac rewrites it — stated as current fact.

  3. 3. “So should I replace every + with a StringBuilder?

    Testing: Does the rule come with a scope, or is it cargo cult?

    Scoring

    Pass: No. One concatenation expression is a single call; rewriting it is longer and no faster. Builders are for loops, or appending across several statements or conditionally.

    Fail: Yes, always — no distinction between one expression and a loop.

  4. 4. “StringBuilder or StringBuffer?

    Testing: Whether they know what the lock actually buys.

    Scoring

    Pass: StringBuilder essentially always. StringBuffer synchronizes every method and only makes individual calls atomic — two threads appending still interleave unpredictably, so it does not fix a shared-buffer design.

    Fail: 'StringBuffer when multithreaded' with no examination of whether that helps.

  5. 5. “What is StringBuilder's default capacity and how does it grow?

    Testing: Concrete numbers.

    Scoring

    Pass: 16 characters, growing to 2n+2 on overflow, so 16 becomes 34. Each grow allocates and copies, so pass the size to the constructor when you know it. new StringBuilder("abc") starts at 19 — the seed plus 16.

    Fail: Guesses 10, or has no number.

  6. 6. “sb1.equals(sb2) with identical contents — true or false?

    Testing: A detail almost nobody has checked.

    Scoring

    Pass: False. StringBuilder does not override equals or hashCode, so it is identity comparison. Compare toString(), or use compareTo which arrived in Java 11.

    Fail: True.

  7. 7. “What does + do with a null, versus concat?

    Testing: Where silent nulls in logs come from.

    Scoring

    Pass: + converts null to the four characters 'null'. concat throws NullPointerException. That is why nulls quietly reach logs as text instead of failing.

    Fail: Thinks both throw, or both produce 'null'.

  8. 8. “How do you join a list with commas?

    Testing: Java 8 awareness, and the trailing-delimiter bug.

    Scoring

    Pass: String.join, StringJoiner if a prefix and suffix are needed, or Collectors.joining in a stream. The append-a-comma-then-delete-the-last-one pattern is the wrong answer and it throws on an empty list.

    Fail: Describes the manual append-and-delete loop as the normal approach.

Score yourself

8/8 — you can hold this at senior level despite it being a junior question 6-7 — solid; the version and capacity details are where you lost points 4-5 — you know String is immutable but not what follows from it 0-3 — run the Warm-up and watch the ratio, then reread "Understand It"

← Back to What is the difference between String, StringBuilder and StringBuffer?