FastPrepFormat Chemical Symbols
Problems/Format Chemical Symbols

Format Chemical Symbols

Morgan Stanley logoMorgan Stanley● EasyINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Morgan Stanley online assessment for interns. The author gives an unsorted array of element symbols. For example, [Cl,Mg,Cl] gives Cl2Mg. Here we chose symbols in lexicographic order, with the count 1 left out. We also defined a symbol-label interface with limits. It does not parse a chemical formula or check a periodic table.

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

Given the unsorted string array symbols, combine equal symbols and return one formatted string. Write each distinct symbol in lexicographic order, followed by its total number of occurrences only when that count is greater than 1. Write decimal counts without separators.

For this exercise, each symbol is an uppercase ASCII letter optionally followed by one lowercase ASCII letter. Treat these as case-sensitive labels; periodic-table validity is not checked. Lexicographic order compares characters in their ordinary ASCII order, and a proper prefix comes first.

For example, H precedes He, and C precedes Ca and Cl. Do not use carbon-first or occurrence-first ordering. Return the empty string for an empty array, and leave the input array unchanged.

Function

formatChemicalSymbols(symbols: String[]) → String

Examples

Example 1

symbols = ["Cl","Mg","Cl"]return = "Cl2Mg"

Cl occurs twice and Mg occurs once. Cl precedes Mg, giving Cl2Mg with no trailing count 1.

Example 2

symbols = ["Na","H","O","H","Na","H"]return = "H3Na2O"

H occurs three times, Na twice and O once. The sorted symbols H, Na, O give H3Na2O.

Constraints

  • For this exercise, assume 0 <= symbols.length <= 2000.
  • Every symbol has length 1 or 2.
  • Every symbol consists of one uppercase ASCII letter A through Z, optionally followed by one lowercase ASCII letter a through z.

More Morgan Stanley Online Assessment questions

  • Three-Resource 0/1 KnapsackOA · Seen Sep 2026
  • Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
  • Maximum Total of Nonadjacent RatingsOA · Seen Jul 2026
  • Count Shipment SchedulesOA · Seen Apr 2026
  • Delete and EarnOA · Seen Apr 2026
  • Interleave Array HalvesOA · Seen Apr 2026
  • Get Patch SequenceOA · Seen Feb 2025
  • String Subsequences 🐳OA · Seen Mar 2024
  • Binary Tree Level OrderOA · Seen Feb 2024
  • Merge IntervalsOA · Seen Feb 2024

Similar questions at other companies

  • Count User Mentions in Chat MessagesRamp · OARamp interview questions
  • Blood Type Inheritance: Possible Parent GenotypesAbridge · OAAbridge interview questions
  • Count Repeated Request IDs (Data Science OA)McKinsey & Company · OAMcKinsey & Company interview questions
  • Minimum Magic Stones After MergesElevenLabs · OAElevenLabs interview questions
  • Flat JSON Value DifferencesChubb · OAChubb interview questions
  • Items SortAkuna Capital · OAAkuna Capital interview questions
  • Count Cross-Array Target-Sum PairsVisa · OAVisa interview questions
  • Get Search ResultsJP Morgan Chase · OAJP Morgan Chase interview questions
  • Lexicographic Word FrequenciesUpstart · OAUpstart interview questions
  • Array Rank TransformTata Consultancy Services · OATata Consultancy Services interview questions

Interview reports that mention this question

  • Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 of 5)

Morgan Stanley interview experiences

  • Morgan Stanley Apprenticeship and Tech Interviews: 6 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 Tech Rounds: 5 Experiences (Part 2 of 5)
All Morgan Stanley interview questionsSee Morgan Stanley hiring insights
Saved
class Solution {
    public String formatChemicalSymbols(String[] symbols) {
        // Write your code here.
    }

}
Checking account…