Find And Replace in String
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
You are given a string s and k replacement operations described by three parallel arrays indices, sources, and targets. Operation i says: if sources[i] occurs in the **original** s starting exactly at index indices[i], replace that occurrence with targets[i]; otherwise do nothing.
All operations are checked against the original string and applied at the same time, so one replacement never affects whether another matches, and indices never shift. The operations are not necessarily listed in order of index.
The ranges [indices[i], indices[i] + sources[i].length) of different operations never overlap. Return the string after all replacements.
Examples
Constraints
- `1 <= s.length <= 1000`
- `k == indices.length == sources.length == targets.length`
- `1 <= k <= 100`
- `0 <= indices[i] < s.length`
- `1 <= sources[i].length <= s.length - indices[i]`, `1 <= targets[i].length <= 50`
- All strings contain only lowercase English letters, and operation ranges do not overlap
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.