FastPrepString Challenge (Generic)
Problems/String Challenge (Generic)

String Challenge (Generic)

TikTok logoTikTok● HardNEW GRADOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

Special thanks: Thanks to Da Best Spike!⸜(。˃ ᵕ ˂ )⸝♡

At the TikTok Academy, students are learning how to improve video captions.

You are given a string caption that only has small Englsh letters ('a' to 'z'). You can make two kinds of changes to caption at any letter as many times as needed.

  • Change the letter to the one right before it in the alphabet. You cannot change 'a' because it is already the first letter 🥲
  • Change the letter to the one right after it in alphabet. You cannot change 'z' as it is already the last letter 🥹

For example, you can change 'f' to 'e' (the letter before) or 'g' (the letter after).

What is your task this time? They want you to make that every letter in the caption has at least one same letter next to it. For example, "aaabb" and "aaccdd" are goooood, but "abaaa" and "abcdef" are veryyy bad 😭

Plz return the min num of changes needed to make the caption meets the rules mentioned above.

Function

stringChallenge(caption: String) → int

Examples

Example 1

caption = "aca"return = 2

You can change 'c' to 'b' (the letter before 'c'), then 'b' to 'a' (the letter before 'b'). Now the caption is 'aaa', which meets the rule. (This line of text might be slightly diff from the original explanation, but it should do well at capturing the meaning of the original explanation :) You will need 2 changes to meet the rule, which is min possible. So the ans is 2 :)

Example 2

caption = "abcdef"return = 3

You can change caption[0] from 'a' to 'b' (the letter after :) cpation[3] from 'd' to 'c' (the letter before) and caption[4] from 'e' to 'f' (the letter after) now the caption is "bbccff", which meets the rule. In total, you'd need 3 chagnes to meet the rule, which is minimum possible. So, the answer is 3. (I think 2 is the typo on the image source)

Example 3

caption = "abab"return = 2

You can change caption[0] from 'a' to 'b' (the letter after) change caption[3] from 'b' to 'a' (the letter before :) Now the caption is "bbaa", which meets the rule. In total, you need 2 changes to meet the rule, which is the min possible. So, return 2 as answer :P

Constraints

  • 2 <= |caption| <= 105

More TikTok interview questions

  • Center of the Largest Perfect X-ShapeOA · Seen Nov 2026
  • Count Subarrays Matching a Comparison PatternOA · Seen Nov 2026
  • Count Suffix Word PairsOA · Seen Nov 2026
  • Minimum Cost of Every K-Capable Model SetOA · Seen Nov 2026
  • Longest Substring Without Repeating CharactersPHONE SCREEN · Seen Oct 2026
  • Next Greater Indices and Maximum GapPHONE SCREEN · Seen Oct 2026
  • Count Subarrays with at Least K Equal-Fruit PairsOA · Seen Oct 2026
  • Shell Command Counts with History ReferencesOA · Seen Oct 2026
  • Product Minus Sum of DigitsOA · Seen Aug 2026
  • Count 2x2 Submatrices by Black CellsOA · Seen Aug 2026

Similar questions at other companies

  • Lexicographically Smallest Grid Path with One Letter ChangeInfosys · OAInfosys interview questions
  • Minimum Merge ConflictsAmazon · OA
  • String Formation (Also for AI/ML Software Engineer Intern :)Snowflake · OASnowflake interview questions
  • Longest Subsequence which is a SubstringSalesforce · OASalesforce interview questions
  • Distinct MovesMeesho · OA
  • Compressed Representation SizeMicrosoft · OAMicrosoft interview questions
  • Find Minimum OperationsIBM · OAIBM interview questions
  • Maximize the Lottery IDCitadel · OACitadel interview questions
  • Sum of All ValuesFlexport · OAFlexport interview questions
  • Maximum Crayon Removal ScoreWells Fargo · OAWells Fargo interview questions

TikTok interview experiences

  • TikTok AI Infra Intern LLM Round 2: 1 Experience
  • TikTok AI Infra Intern DeepSpeed Round: 1 Experience
  • TikTok AI Infra Intern Kernel Round: 1 Experience
  • TikTok AI Infra Intern Interviews, 3 Rounds: 1 Experience
  • TikTok AI Infra Intern Interview Timeline: 1 Experience
  • TikTok AI Infra Intern Onsite Round 2: 1 Experience
All TikTok interview questionsSee TikTok hiring insights
Saved
class Solution {
    public int stringChallenge(String caption) {
      // write your code here
    }

}
Checking account…