Shift Every K-th Consonant
Reported by candidates from TikTok's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
TikTok reported this one in July 2025, and it looks like a throwaway string problem until one letter trips you. Shift Every K-th Consonant sounds like a simple substitution cipher. It's really a counting problem with a nasty detail: the next consonant isn't always the next letter. If you're taking this OA in the next couple of days, read the edge cases first. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the logic here is short enough to own before you open the editor.
The problem
You are implementing a simple substitution cipher that affects only consonant characters in a string memo. Read the consonants in memo from left to right. Every k-th consonant is shifted to the following consonant in the alphabet, while vowels and all other characters remain unchanged. In other words, consonants numbered k, 2 * k, 3 * k, and so on are shifted. The consonants are b, c, d, f, g, h, j, k, l, m, n, p, q, r, s, t, v, w, x, y, and z. When shifting from z, wrap around to b. Preserve the case of every shifted character. Return the transformed memo. A solution with time complexity no worse than O(memo.length^2) will fit within the execution time limit. Function solution(memo: String, k: int) → String Examples Example 1 memo = "CodeSignal" k = 3 return = "CodeTignam" The consonants are C, d, S, g, n, and l. The third consonant S shifts to T, and the sixth consonant l shifts to m. Example 2 memo = "Quiz, Citizenship, puZZle" k = 5 return = "Quiz, Citibenship, quZZle" The fifth consonant is the lowercase z in Citizenship, so it wraps to b. The tenth consonant is the first p in puZZle, so it shifts to q.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The hinted pattern says binary search, but the problem doesn't need it. It's a single linear pass with a counter. Walk the string, and for each consonant (case-insensitive) increment the count. When count % k == 0, replace it with the next consonant. The trap is that the 'next consonant' skips vowels. b goes to c, d goes to f, h goes to j, n goes to p, t goes to v, z wraps to b. Build a lookup map from each consonant to its successor, and don't use char + 1. Preserve case by checking uppercase before mapping. Only consonants advance the counter, so vowels, spaces, commas and digits must not count. Example 2 catches people who count every letter. The input is small enough that O(n) easily beats the quadratic allowance. If you freeze live, StealthCoder can hand you the map-and-counter skeleton, but you should be able to write it from memory.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Shift Every K-th Consonant 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass TikTok's OA.
TikTok reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shift Every K-th Consonant FAQ
What's the trick in Shift Every K-th Consonant?+
Build a successor map of the 21 consonants so each one points to the next consonant, not the next letter. Then count only consonants and apply the map when the count is divisible by k. The map handles the vowel gaps and the z to b wrap in one place.
Do I need binary search for this TikTok OA problem?+
No. Despite the binary-search hint, a single left-to-right pass is enough. The statement even allows O(n^2), so linear is far under the limit. Spend your effort on the counting rules and the successor mapping, not on a search structure.
What edge cases break a naive solution?+
Using char + 1 is the big one, since d should become f and not e. Others: forgetting z wraps to b, losing uppercase on shifted letters, and counting vowels or punctuation toward k. Also check k = 1, where every consonant shifts.
How should I handle uppercase and lowercase?+
Lowercase the character to look it up in the consonant map, then restore case if the original was uppercase. Keep the counter case-insensitive so 'Z' and 'z' both count as consonants. Non-letters pass through untouched and never increment the count.
How do I prepare for this in 48 hours?+
Write the solution twice from scratch. Then test Example 1 (CodeSignal, k=3) and Example 2 (the z to b wrap) by hand. Add a test with k larger than the consonant count, where nothing changes. That covers nearly every way this problem fails.