FastPrepRun-Length Encode Consecutive Letter Runs
Problems/Run-Length Encode Consecutive Letter Runs

Run-Length Encode Consecutive Letter Runs

Morgan Stanley logoMorgan Stanley● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Morgan Stanley onsite interview for a full-time role. The author gives the example AAAABBCC turning into A4B2C2. Here the input is uppercase English letters. Every maximal run gets a decimal count, even a count of 1. An empty string gives empty output. We also set length limits. Separate runs of the same letter stay separate.

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

Run-length encode s by replacing each maximal consecutive run of the same character with that character followed by its decimal run length. Process the runs from left to right.

For this exercise, s contains only uppercase English letters A through Z. Always include the count, even when it is 1. Do not combine separated runs of the same letter and do not return global character frequencies. An empty string encodes to an empty string. Leave the input unchanged.

Function

runLengthEncode(s: String) → String

Examples

Example 1

s = "AAAABBCC"return = "A4B2C2"

The maximal consecutive runs are AAAA,BB,CC, with lengths 4,2,2. Encoding in order gives A4B2C2.

Example 2

s = "ABBA"return = "A1B2A1"

The runs are A,BB,A. The separated A runs are not merged, and count 1 is included, giving A1B2A1.

Constraints

  • For this exercise, assume 0 <= s.length <= 2000.
  • Every character is an uppercase English letter A through Z.

More Morgan Stanley Onsite Interview questions

  • Generate Strings of Length at Least ThreeONSITE INTERVIEW · Seen Sep 2026
  • Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
  • Kth Largest Array ElementONSITE INTERVIEW · Seen Jul 2026
  • Maximum Edge Removals for Two TravelersONSITE INTERVIEW · Seen Oct 2025
  • Remove K DigitsONSITE INTERVIEW · Seen Oct 2025
  • Count Ideal ArraysONSITE INTERVIEW · Seen Aug 2023
  • Distinct Palindrome PartitionsONSITE INTERVIEW · Seen Aug 2023
  • LFU Cache OperationsONSITE INTERVIEW · Seen Aug 2023
  • All-Pairs Shortest PathsOA · ONSITE INTERVIEW · Seen Feb 2022
  • Fibonacci with Input ValidationONSITE INTERVIEW · Seen Feb 2022

Similar questions at other companies

  • Bounded Word Guess SolverAtlassian · ONSITE INTERVIEWAtlassian interview questions
  • Document Edit EventsClickUp · ONSITE INTERVIEW
  • Predict Runtime Method DispatchAMD · ONSITE INTERVIEWAMD interview questions
  • Basic String Compression with Length GuardNew Relic · PHONE SCREEN · ONSITE INTERVIEW
  • Print Sentences as TableAirbnb · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
  • Retry with an Attempt LimitOracle · ONSITE INTERVIEWOracle interview questions
  • Extract Error Log ContextFlexTrade · ONSITE INTERVIEWFlexTrade interview questions
  • String Pattern ReplacementGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • IPv4 Forward IteratorOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Account Balance Manager Part 2 - Reject OverdraftsStripe · ONSITE INTERVIEWStripe interview questions

Interview reports that mention this question

  • Morgan Stanley Apprenticeship and Tech Interviews: 6 Experiences

Morgan Stanley interview experiences

  • Morgan Stanley New Grad Technology Analyst: 2 Experiences
  • Morgan Stanley Intern Oldest Loops: 5 Experiences (Part 5 of 5)
  • Morgan Stanley Intern OOP Loops: 5 Experiences (Part 4 of 5)
  • Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 of 5)
  • Morgan Stanley Intern Tech Rounds: 5 Experiences (Part 2 of 5)
All Morgan Stanley interview questionsSee Morgan Stanley hiring insights
Saved
class Solution {
    public String runLengthEncode(String s) {
        // Write your code here.
    }

}
Checking account…