Count Pairs
Reported by candidates from DataBricks's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
DataBricks asked this in August 2024, and it looks deceptively simple until you realize the real work is string comparison with a twist. You need to count pairs (x, y) where fruits[x] can be obtained from fruits[y] by swapping at most 2 digits. The catch: you're not rearranging strings, you're checking if one is a digit-swap neighbor of the other. Most candidates miss the exact scope of what "swapping no more than 2 digits" means, and that's where StealthCoder becomes your safety net if you blank on the edge cases.
The problem
\ \ There is an array of fruits. Your goal is to calculate the amount of distinct pairs (x, y) so that\ 0. Note that fruits[x] could be gotten from fruits[y] by swapping no more than\ 2 digits of fruits[y] :)\ \ Note again:: One might not make swapping to any digits at all, so, if x and fruits[x] = fruits[y], such pair\ should be counted. \
Reported by candidates. Source: FastPrep
Pattern and pitfall
This is a hash-table and counting problem disguised as a string problem. The algorithm is straightforward: for each pair (x, y) where x < y, check if fruits[x] and fruits[y] differ by at most 2 digit swaps. The tricky part is the comparison function. You need a helper that counts the minimum number of single-digit swaps to transform one string into another. If that count is 0, 1, or 2, the pair counts. Build a set or map to avoid duplicates, then iterate efficiently. Common pitfall: confusing digit-swap with character rearrangement. This is a direct comparison, not an anagram check. The time complexity is O(n^2 * m) where n is array length and m is string length. StealthCoder handles the helper logic when your recall falters under pressure.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Count Pairs 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass DataBricks's OA.
DataBricks reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Pairs FAQ
What exactly does 'swapping 2 digits' mean here?+
It means you pick two positions in the string and exchange the characters at those positions. A swap reduces the edit distance by up to 2 if done optimally. You're checking: can I transform fruits[y] into fruits[x] with 0, 1, or 2 such swaps.
Do I need to check all permutations of swaps?+
No. You're not generating permutations. You're counting the minimum swaps needed between two fixed strings. Use a position-by-position comparison: identify mismatches, then check if each mismatch pair can resolve with one swap. If two or fewer mismatches exist and they resolve, count it.
Is this a DP or a graph problem?+
Neither. It's counting with a custom comparator. Hash-table for deduplication, nested loop for pairs, helper function for the swap-distance check. No recursion, no graph structure needed.
What if fruits[x] equals fruits[y]?+
That counts as 0 swaps, so the pair is valid. The problem explicitly says 'one might not make swapping to any digits at all,' so identical pairs are included.
How do I verify my swap-distance function is correct?+
Test edge cases: identical strings (0 swaps), one swap needed ('ab' vs 'ba'), two swaps needed, and strings that can't transform in 2 swaps. Run through a few by hand before submitting.