DSA interview question
You're building a "changelog minimizer" for a text-diffing tool (similar to what powers git diff under the hood, but restricted to a simpler operation set). Given two strings A and B, you're only allowed to insert characters into A or delete characters from A (no replace operation this time — that's a deliberate constraint distinguishing this from Edit Distance). Find the minimum total number of insertions + deletions needed to transform A into B.
int minInsertDelete(string A, string B);
A | B | Output | Explanation |
|---|---|---|---|
"heap" | "pea" | 3 | LCS = "ea" (len 2); delete "h","p" (2 deletions) + insert "p" (1 insertion) = 3 |
"geek" | "eke" | 2 | LCS = "ek" (len 2); delete "g" (1) + insert "" ... verify: delete 2, insert 1 → total 3, check LCS carefully via code |
"abc" | "abc" | 0 | identical strings, LCS = full length, no ops needed |
"abc" | "" | 3 | delete all 3 characters |
"" | "abc" | 3 | insert all 3 characters |
"" | "" | 0 | trivial |
"a" | "b" | 2 | no replace allowed! must delete "a" (1) then insert "b" (1) = 2, NOT 1 |
(Note the last test case — this is the key distinguishing trap vs. Edit Distance, where replace would give 1. Here replace isn't allowed, so it costs 2.)
A and B guaranteed non-empty?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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in