PPractick

DSA interview question

Isomorphic Strings

Easy · Algorithms · Topics: string, hash map

The problem

A cipher checker receives a plaintext s and a ciphertext t and must decide whether t could come from s by a consistent one-to-one letter substitution: every occurrence of a letter maps to the same replacement, and no two different letters share a replacement. For example "egg" → "add" works (e→a, g→d), but "foo" → "bar" doesn't (o can't map to both a and r). The mapping has to be checked in both directions; forgetting the reverse direction is the classic bug.

Example: ("paper", "title") → true; ("badc", "baba") → false.

Function signature

bool isIsomorphic(const string& s, const string& t);

Test cases

Input (s, t)OutputExplanation
("egg", "add")truee→a, g→d
("foo", "bar")falseo would map to both a and r
("paper", "title")truep→t, a→i, e→l, r→e
("badc", "baba")falseb→b and d→b: two letters share a target
("a", "a")true
("ab", "aa")falseThe reverse direction fails

(Confirm: s and t have the same length; characters are ASCII; a character may map to itself.)

How interviewers phrase it

Clarifying questions to ask

  1. Are the strings the same length? (If not, false.)
  2. Which character set: lowercase letters, ASCII, Unicode?
  3. May a character map to itself?

Follow-ups to expect

  1. "Why check both directions?"
  2. "Is there a trick with one pass and no maps?"
  3. "What's another characterisation?"
  4. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in