LearnThatStack Ace your next interview
Arrays, Strings & Hash Tables · question
Question 10 of 85

What is a string, and why are strings immutable in many languages?

beginner
← All Arrays, Strings & Hash Tables questions
Re-explain

A string is an ordered sequence of characters, usually stored as bytes that encode text. In many languages it behaves like a read-only array of characters that you can index and iterate over.

Immutability means that once a string is created, its contents never change. Operations that look like edits, such as replacing a character, build a brand new string and leave the original untouched.

Languages make this choice for safety and speed. Immutable strings can be shared freely between threads with no locking, and their hash code can be computed once and reused. The tradeoff is that heavy in-place editing wastes work creating copies. That is why building text in a loop should use a string builder instead.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Arrays, Strings & Hash Tables One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

What you can say

  1. A string is an ordered sequence of characters, stored as bytes that encode text, and you can index and iterate over it like a read-only array.
  2. Immutable means that once a string is created, its contents never change.
  3. Anything that looks like an edit, say replacing one character, builds a brand new string and leaves the original alone.
  4. Languages pick this for safety and speed, since threads can share a string with no locking and its hash code is computed once and reused.
  5. The cost is a fresh copy on every edit, so when I build text in a loop I use a string builder instead.

Weak answers to avoid

  • Claims replace or += edits the string in place That is the exact misconception the question tests. Say that each edit returns a new string and the original stays untouched.
  • Says 'it's safer' with no mechanism Vague safety claims cost points. Name what immutability buys, such as sharing across threads with no locks and a hash code computed once.
  • Presents immutability as free Every edit builds a copy, so += in a loop gets slow. Name that cost and the fix, a string builder.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Arrays, Strings & Hash Tables cheatsheet.

← Back to all Arrays, Strings & Hash Tables questions
Pro · $10/mo

74 of 85 Arrays, Strings & Hash Tables answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime