Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
A distinct string is a string that is present only once in an array.
Given an array of strings arr, and an integer k, return the kth distinct string present in arr. If there are fewer than k distinct strings, return an empty string "".
Note that the strings are considered in the order in which they appear in the array.
Example 1:
Input: arr = ["d","b","c","b","c","a"], k = 2 Output: "a" Explanation: The only distinct strings in arr are "d" and "a". "d" appears 1st, so it is the 1st distinct string. "a" appears 2nd, so it is the 2nd distinct string. Since k == 2, "a" is returned.
Example 2:
Input: arr = ["aaa","aa","a"], k = 1 Output: "aaa" Explanation: All strings in arr are distinct, so the 1st string "aaa" is returned.
Example 3:
Input: arr = ["a","b","a"], k = 3 Output: "" Explanation: The only distinct string is "b". Since there are fewer than 3 distinct strings, we return an empty string "".
Constraints:
1 <= k <= arr.length <= 10001 <= arr[i].length <= 5arr[i] consists of lowercase English letters.Problem summary: A distinct string is a string that is present only once in an array. Given an array of strings arr, and an integer k, return the kth distinct string present in arr. If there are fewer than k distinct strings, return an empty string "". Note that the strings are considered in the order in which they appear in the array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
["d","b","c","b","c","a"] 2
["aaa","aa","a"] 1
["a","b","a"] 3
count-common-words-with-one-occurrence)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2053: Kth Distinct String in an Array
class Solution {
public String kthDistinct(String[] arr, int k) {
Map<String, Integer> cnt = new HashMap<>();
for (String s : arr) {
cnt.merge(s, 1, Integer::sum);
}
for (String s : arr) {
if (cnt.get(s) == 1 && --k == 0) {
return s;
}
}
return "";
}
}
// Accepted solution for LeetCode #2053: Kth Distinct String in an Array
func kthDistinct(arr []string, k int) string {
cnt := map[string]int{}
for _, s := range arr {
cnt[s]++
}
for _, s := range arr {
if cnt[s] == 1 {
k--
if k == 0 {
return s
}
}
}
return ""
}
# Accepted solution for LeetCode #2053: Kth Distinct String in an Array
class Solution:
def kthDistinct(self, arr: List[str], k: int) -> str:
cnt = Counter(arr)
for s in arr:
if cnt[s] == 1:
k -= 1
if k == 0:
return s
return ""
// Accepted solution for LeetCode #2053: Kth Distinct String in an Array
use std::collections::HashMap;
impl Solution {
pub fn kth_distinct(arr: Vec<String>, mut k: i32) -> String {
let mut cnt = HashMap::new();
for s in &arr {
*cnt.entry(s).or_insert(0) += 1;
}
for s in &arr {
if *cnt.get(s).unwrap() == 1 {
k -= 1;
if k == 0 {
return s.clone();
}
}
}
"".to_string()
}
}
// Accepted solution for LeetCode #2053: Kth Distinct String in an Array
function kthDistinct(arr: string[], k: number): string {
const cnt = new Map<string, number>();
for (const s of arr) {
cnt.set(s, (cnt.get(s) || 0) + 1);
}
for (const s of arr) {
if (cnt.get(s) === 1 && --k === 0) {
return s;
}
}
return '';
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.