DSA interview question
You're building a plagiarism detection system for an online coding judge (think: detecting if two submitted code files share large identical chunks, or detecting duplicate questions in a Q&A platform). Comparing substrings character-by-character every time is too slow when you need to do millions of comparisons across huge texts. Your tech lead asks you to explain — conceptually and with code — how string hashing (specifically polynomial rolling hash, as used in Rabin-Karp) lets you compare substrings in O(1) after O(n) preprocessing, and why this is risky (collisions) compared to deterministic methods like KMP/Z-function.
This is a theory/conceptual topic — interviewers use it to test whether you understand the why behind hashing, not just whether you can paste a formula.
(Theory questions typically still expect you to implement core utilities — here are the standard ones to know cold.)
// Precompute hash array and power array for a string
void computeHashes(const string& s, vector<long long>& hashArr, vector<long long>& powArr, long long mod, long long base);
// Get hash of substring s[l..r] (inclusive) in O(1) using precomputed prefix hashes
long long getSubstringHash(int l, int r, const vector<long long>& hashArr, const vector<long long>& powArr, long long mod);
// Compare if two substrings are equal using hashing
bool compareSubstrings(int l1, int r1, int l2, int r2, const vector<long long>& hashArr, const vector<long long>& powArr, long long mod);
| Scenario | Expected Behavior |
|---|---|
s = "abcabc", compare s[0..2] ("abc") and s[3..5] ("abc") | Hashes equal → substrings equal |
s = "abcabd", compare s[0..2] ("abc") and s[3..5] ("abd") | Hashes differ → substrings different |
| Two different strings producing the same hash (collision) | False positive — hashing says "equal" but they're not — must discuss mitigation |
| Very large text (10⁶ chars), repeated substring queries | O(1) per query after O(n) preprocessing — show why this beats O(n) per naive compare |
| Single character string | Hash = value of that one character (base case) |
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