🌸 Source note: This problem is backed by true source. FastPrep's current version matches about 80-85% of the original interview question based on the available source details.
The original prompt described a string with characters such as a, b, and c, and asked for the closest c for each position. For a general version, given a string s and a single-character string target, return the index of the closest occurrence of target for every index in s.
Return answer where answer[i] is the closest target index to i. If two target indices are equally close, return the smaller index. If the target never appears, return -1 for every position.
Complete the function closestTargetIndices in the editor.
closestTargetIndices has the following parameters:
String s: the input string.String target: a single-character string representing the target character.
Returns
int[]: an integer array with length s.length. Each value is the closest target index for that position, or -1 when the target does not appear.
Clarifications / Corner Cases
- Return the index of the closest target, not the character itself.
- The output length must equal
s.length. - If
sis empty, return an empty array. - If
targetdoes not appear ins, return-1for every position. - If
s[i]is the target,answer[i]should bei. - If two target positions are equally close, use the smaller index.
- The string may contain characters other than
a,b, andc; the original prompt usedcas the target example. - Matching is case-sensitive when the input contains letters with different cases.
- The target may appear once, multiple times, consecutively, at the beginning, or at the end.
- The string may be very long, so scanning left and right from every index may be too slow.
Follow-up / Interview Discussion
- If you need to answer the closest-target query for every index, what can be precomputed?
- If there are many repeated queries on the same string and same target, how would preprocessing help?
- What changes if the interviewer asks for distance instead of target index?
- What changes if the tie-break rule asks for the larger index instead of the smaller index?
- If the string arrives as a stream, can you output the exact closest target for each position immediately?
- If delayed output is allowed in a stream, when can earlier positions be finalized?
- If the stream is too large to store entirely, what information do you still need to keep?
s = "abccabac" target = "c" return = [2, 2, 2, 3, 3, 3, 7, 7]
Index 5 is equally far from target indices 3 and 7, so the smaller index 3 is returned.
s = "abab" target = "c" return = [-1, -1, -1, -1]
The target does not appear anywhere in the string.
s = "caaac" target = "c" return = [0, 0, 0, 4, 4]
Index 2 is the same distance from target indices 0 and 4, so the smaller index 0 is returned.
s = "abccba" target = "c" return = [2, 2, 2, 3, 3, 3]
Each target index returns itself; positions around the target block point to the closest edge of the block. The source screenshot's output for this example appears to list 4 for the last two positions, but by the stated rule their closest target index is 3, so this practice version uses the corrected output.
s = "" target = "c" return = []
The output is also empty.
0 <= s.length <= 100000targetis a single-character string.smay contain any printable ASCII characters unless otherwise specified by the interviewer.- Return values must be valid indices in
s, or-1.
- Horizontal Pod AutoscalerSeen Jul 2026
- Minimum HeightOA · Seen Jul 2026
- Vowel SubstringSeen Jun 2026
- String Formation (Also for AI/ML Software Engineer Intern :)OA · Seen Jun 2026
- Drawing EdgeOA · Seen Jun 2026
- Effective Role PrivilegesPHONE SCREEN · Seen May 2026
- Closest Bathroom / Desk on a GridPHONE SCREEN · Seen May 2026
- Minimum Clicks Between Wiki PagesOA · Seen May 2026
public int[] closestTargetIndices(String s, String target) {
// write your code here
}