Top Two Alphabetic Words
Updated
Problem statement
Given a string text representing file contents, return up to two distinct valid words ranked by frequency. For this exercise, split text at one or more ASCII whitespace characters: space, tab, newline, carriage return, form feed or vertical tab. A token is valid only if every character is an ASCII English letter A-Z or a-z. Discard an entire token containing a digit or punctuation; do not extract letter fragments.
Count words case-sensitively. Rank valid words by occurrence count descending, then by their last occurrence in the token sequence descending. Thus a more recently occurring word wins a frequency tie. Return the word strings in that order. If fewer than two distinct valid words exist, return all that exist; return an empty array when none exist. The input is file contents supplied as a string, and no file path is needed.
Function
topTwoWords(text: String) → String[]Examples
Example 1
text = "1. H1i hen 1ss you hen he s you hen he he 1ss 1ss"return = ["he", "hen"]The valid counts are he: 3, hen: 3, you: 2, and s: 1. The final he occurs after the final hen, so the tied leaders are returned as [he, hen]. Tokens 1., H1i, and 1ss are rejected entirely.
Example 2
text = "cat dog cat dog eel"return = ["dog", "cat"]Both cat and dog occur twice. The final dog follows the final cat, so dog ranks first and cat second. The word eel occurs once.
Constraints
- For this exercise, assume 0 <= text.length <= 10000.
- Every input character is printable ASCII from codes 32 through 126, or an ASCII whitespace control with codes 9 through 13.
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
- Run-Length Encode Consecutive Letter RunsONSITE 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
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Longest Consecutive Login StreakAmazon · ONSITE INTERVIEW
- Keyboard Autocomplete SuggestionsLyft · ONSITE INTERVIEWLyft interview questions
- Query JSON Records with WHERE and ORDER BYGitHub · ONSITE INTERVIEW
- Sort by Odd-Position Digit SumPelatro · ONSITE INTERVIEW
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
- Serialize and Deserialize a String Key-Value StoreByteDance · ONSITE INTERVIEWByteDance interview questions
- Group AnagramsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Most Frequent Call Stack Per ThreadRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Build an Inverted Document IndexConfluent · ONSITE INTERVIEWConfluent interview questions
Interview reports that mention this question
Morgan Stanley interview experiences
- Morgan Stanley Apprenticeship and Tech Interviews: 6 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)