FastPrepCollapse Runs of Repeated Characters
Problems/Collapse Runs of Repeated Characters

Collapse Runs of Repeated Characters

Nutanix logoNutanix● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in the first video round for a Nutanix internal tools intern. The coding task was typed in a Google Doc. It was to squeeze each block of the same letter down to one letter. A letter that comes back later is kept. The author was reminded to talk through the plan before coding. There were two follow-ups: the choice of StringBuilder in Java, and saving a bit of extra space.

The report gives no input limits. Here s has 1 to 10^5 lowercase letters, you return the new string, and the examples are our own. The follow-ups are covered in the editorial and are not tested.

Our version is about 90% the same as the reported question.

You are given a string s of lowercase English letters. A run is a group of one or more equal letters that stand next to each other. Replace every run with a single copy of its letter and return the new string.

Only neighbors count. If a letter shows up again after a different letter, it starts a new run, so it appears again in the answer.

Function

collapseRuns(s: String) → String

Examples

Example 1

s = "bbbookkeeper"return = "bokeper"

The runs are bbb, oo, kk, ee, p, e and r. Each run becomes one letter, so the answer is "bokeper". The second e stays because a p sits between it and the first group of es.

Example 2

s = "mississippi"return = "misisipi"

The pairs ss, ss and pp each become one letter. Every other letter is already a run of length 1, so the answer is "misisipi". The letter s appears twice in the answer because the two pairs are separate runs.

Example 3

s = "zzzz"return = "z"

The whole string is one run of z, so the answer is "z".

Constraints

  • 1 <= s.length <= 10^5
  • s consists only of lowercase English letters (a to z).

More Nutanix Onsite Interview questions

  • Design Hit CounterONSITE INTERVIEW · Seen Oct 2026
  • Peak Process MemoryONSITE INTERVIEW · Seen Aug 2026
  • Minimum Operations to Transform ArrayONSITE INTERVIEW · Seen Jun 2026
  • Count Connected Components in an Undirected GraphONSITE INTERVIEW · Seen Aug 2025
  • LRU CacheONSITE INTERVIEW · Seen Aug 2025
  • Running Median of a Multilevel Linked List in Level OrderONSITE INTERVIEW · Seen Aug 2025
  • Custom Array IteratorONSITE INTERVIEW · Seen Dec 2024
  • Anagram Words in a SentenceONSITE INTERVIEW · Seen Apr 2022
  • Sudoku SolverONSITE INTERVIEW · Seen Feb 2022
  • Distance Sums to Special Nodes in a Weighted TreeONSITE INTERVIEW · Seen Aug 2021

Similar questions at other companies

  • Longest Spoken PrefixElevenLabs · ONSITE INTERVIEWElevenLabs interview questions
  • Palindrome Check Ignoring SymbolsWalmart · ONSITE INTERVIEWWalmart interview questions
  • Reverse a String Keeping Dashes in PlaceNykaa · ONSITE INTERVIEWNykaa interview questions
  • Reverse Vowels of a StringGoogle · ONSITE INTERVIEWGoogle interview questions
  • Valid Palindrome After NormalizationKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
  • Count Binary SubstringsDocuSign · ONSITE INTERVIEWDocuSign interview questions
  • Minimum Changes for Palindromic BlocksCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
  • Palindrome Match PercentageApple · ONSITE INTERVIEWApple interview questions
  • Reverse A StringNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
  • Validate a Word AbbreviationSalesforce · ONSITE INTERVIEWSalesforce interview questions

Interview reports that mention this question

  • Nutanix SWE Intern Phone Screens: 20 Experiences (Part 3 of 3)

Nutanix interview experiences

  • Nutanix SWE Intern OA Questions: 16 Experiences (Part 2 of 3)
  • Nutanix U.S. SWE Intern Interviews: 54 Experiences (Part 1 of 3)
  • Nutanix SWE OA and Interviews, Level Not Reported: 9 Experiences
  • Nutanix Intern Debugging Round and Interviews: 25 Experiences
All Nutanix interview questionsSee Nutanix hiring insights
Saved
class Solution {
    public String collapseRuns(String s) {
        // Write your code here.
    }
}
Checking account…