Closest Target Character
Reported by candidates from Snowflake's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Snowflake reportedly served this one in July 2026, and the trap is the tie-break, not the idea. For every index in a string, you return the closest index holding a target character, and ties go to the smaller index. With s up to 100000 characters, scanning outward from each position is a quiet timeout. If your OA invite is two days out, this is a two-pass string problem you can nail in ten minutes. If your brain freezes mid-assessment, StealthCoder runs invisibly on your desktop as a safety net and hands you the solution while the proctor sees nothing.
The problem
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. Function closestTargetIndices(s: String, target: String) → int[] 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 s is empty, return an empty array. If target does not appear in s, return -1 for every position. If s[i] is the target, answer[i] should be i. If two target positions are equally close, use the smaller index. The string may contain characters other than a, b, and c; the original prompt used c as 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? Examples Example 1 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. Example 2 s = "abab" target = "c" return = [-1, -1, -1, -1] The target does not appear anywhere in the string. Example 3 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. Example 4 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. Example 5 s = "" target = "c" return = [] The output is also empty. Constraints 0 <= s.length <= 100000 target is a single-character string. s may contain any printable ASCII characters unless otherwise specified by the interviewer. Return values must be valid indices in s, or -1.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is precomputation. Pass one goes left to right and records the most recent target index at or before each position. Pass two goes right to left and records the nearest target index at or after each position. Then for each i, compare the two candidates by distance. If distances are equal, pick the left one, since it's the smaller index. If only one exists, take it. If neither exists, return -1. The common pitfall is the tie. Example 1 at index 5 is equidistant from 3 and 7, and the answer is 3. Another pitfall is the nested scan that blows up on long input. Also handle an empty string and a target that never appears. Note that example 4's output in the prompt was corrected by the author, so trust the stated rule. If you blank on the tie logic live, StealthCoder is the hedge that surfaces the two-pass version.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Closest Target Character cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Snowflake's OA.
Snowflake reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Closest Target Character FAQ
What's the trick in the Snowflake Closest Target Character problem?+
Precompute two arrays. One holds the nearest target index to the left (including the position itself), the other holds the nearest to the right. Then compare distances per index. That's O(n) time and avoids scanning outward from every position, which is too slow at 100000 characters.
How do I handle ties between two target positions?+
When the left and right candidates are equally far, return the left one because it's the smaller index. Use a check like i - left <= right - i to pick left. Example 3, caaac, at index 2 returns 0 for exactly this reason.
What edge cases should I test before submitting?+
Test an empty string (return an empty array), a target that never appears (all -1), a target only at the start or end, consecutive targets, and positions that are themselves the target (answer is i). Also check case sensitivity, since matching is exact.
Can this be done in one pass or with less memory?+
Yes. Store the target indices in a list, then walk i forward with a pointer to the next target and compare it with the previous one. That's still O(n) and avoids a second full array. The two-pass array approach is simpler and safer under OA pressure.
How do I prepare for this in 48 hours?+
Write the two-pass solution from scratch twice, then run the five examples by hand. Practice the variations the prompt hints at: return distance instead of index, or flip the tie-break to the larger index. Those are one-line changes if you understand the comparison step.