Closest Target Character
Updated
Problem statement
For example, a string has characters such as a, b, and c, and you want the closest c for each position. In general, 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
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. - 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 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 distance is requested 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?
Variant: nearest cake for each person
A related follow-up asks for a closest cake for each person in a one-dimensional row. For this variant, let 0 mean empty, 1 mean person, and 2 mean cake. Return a cake index for each person, in original person order. Use absolute index distance, allow a cake to be selected by more than one person, prefer the smaller cake index on a tie, and use -1 if no cake exists.
To solve it with the callable above, encode the row as a string of the same digits, call closestTargetIndices(encoded, "2"), and retain only answers at original positions containing a person. For example, row [1,0,2,0,1,0,2] becomes "1020102". The all-position result is [2,2,2,2,2,6,6]; selecting original person positions 0 and 4 gives [2,2]. The second person has an equal-distance choice of cake indices 2 and 6, so the smaller-index rule selects 2.
One-to-one cake consumption would be a different allocation problem and is not assumed by this variant. The main function still returns one target index for every string position.
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. By the stated rule, the closest target index for the last two positions is 3.
Example 5
s = ""target = "c"return = []The output is also empty.
Constraints
0 <= s.length <= 100000targetis a single-character string.smay contain any printable ASCII characters unless otherwise specified.- Return values must be valid indices in
s, or-1.
More Snowflake Phone Screen questions
- Merge Two Binary TreesPHONE SCREEN · Seen Nov 2026
- Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
- Minimum Edits to a Perfect Binary TreePHONE SCREEN · Seen Nov 2026
- Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
- Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
- Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
- Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026
- Greedy Person-Cake Matching DistancePHONE SCREEN · Seen Nov 2026
- Tree Height After Child PromotionPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Straight-Line Word SearchReddit · PHONE SCREENReddit interview questions
- Search and Format ContactsAdyen · PHONE SCREENAdyen interview questions
- Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
- Transaction Fee Calculator — Parts 1–2: Basic Fee Calculation and Conditional Fee RulesStripe · PHONE SCREENStripe interview questions
- Valid ParenthesesByteDance · PHONE SCREENByteDance interview questions
- Detect a Straight FlushTemu · PHONE SCREENTemu interview questions
- Check if a String Is a PalindromeCisco · PHONE SCREENCisco interview questions
- Halfway Course in a Single TrackCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Longest Common PrefixKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Format a Signed Integer with CommasHudson River Trading · PHONE SCREENHudson River Trading interview questions