Reverse File Lines
Updated
Problem statement
Return the lines of text in reverse order. LF (\n) separates lines and is omitted from returned strings. Preserve every other character, including spaces and empty lines between separators.
An empty text has no lines. A final LF ends the last line; it does not add an extra empty line. Thus "a\n" has one line, while "a\n\n" has two lines.
Use O(1) auxiliary state, excluding the input text and returned strings. The file-seeking version of this task is discussed in the solution.
Function
reverseLines(text: String) → String[]Examples
Example 1
text = "one\ntwo\nthree"return = ["three","two","one"]The three lines are returned from the last line to the first.
Example 2
text = "a\n\n"return = ["","a"]The second LF ends an empty line. The final LF does not add a third line.
Constraints
- 0 <= text.length <= 100000
- text contains printable ASCII characters or newline (LF).
- A line ends at LF or at the end of the text. A final LF does not create another empty line.
More Citadel Onsite Interview questions
- Maximize Workday EarningsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Minimum Changes for Palindromic BlocksOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Removal PoolONSITE INTERVIEW · Seen Sep 2026
- Centered K-by-K Submatrix SumsONSITE INTERVIEW · Seen Aug 2026
- Search Suggestions with a Prefix TreeONSITE INTERVIEW · Seen Aug 2026
- Traverse Interior Matrix Cells in Row-Major OrderONSITE INTERVIEW · Seen Aug 2026
- Time-Based Key-Value StorePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2026
- 2048 Simulation and Board CodecONSITE INTERVIEW · Seen Jan 2026
- Cancellable Priority Task QueueONSITE INTERVIEW · Seen Jan 2026
- Dynamic Weighted SamplingONSITE INTERVIEW · Seen Jan 2026
Similar questions at other companies
- Shortest Deletion for Distinct CharactersOracle · ONSITE INTERVIEWOracle interview questions
- Word Collection: Distance and AnagramsGoogle · ONSITE INTERVIEWGoogle interview questions
- Validate a Palindrome After Limited DeletionsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Longest Palindromic Substring LengthMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- All Distinct Palindromic SubstringsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- String CompressionBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Longest Palindromic SubstringPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
- Strings at Most One Edit ApartExpedia · ONSITE INTERVIEWExpedia interview questions
- Longest Spoken PrefixElevenLabs · ONSITE INTERVIEWElevenLabs interview questions
- Palindrome Check Ignoring SymbolsWalmart · ONSITE INTERVIEWWalmart interview questions