FastPrepReverse File Lines
Problems/Reverse File Lines

Reverse File Lines

Citadel logoCitadel● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Citadel new grad onsite interview. The interviewer asked to implement tac, which outputs a file line by line in reverse, using only O(1) extra space.

The report mentions C++ file seeking but does not say how line endings work. Here, LF separates lines, a final LF adds no extra line, and empty text has no lines. The input text and the returned lines replace the file stream and output stream. The follow-up about a file API is discussed, and tests do not check it.

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

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

Interview reports that mention this question

  • Citadel SWE Intern Later Rounds and Superday: 13 Experiences

Citadel interview experiences

  • Citadel Securities Quant Researcher Intern Loop: 1 Experience
  • Citadel Securities Research Engineer Interview: 1 Experience
  • Citadel Securities SWE Intern First Round: 2 Experiences
  • Citadel Securities Experienced SWE Interviews: 1 Experience
  • Citadel SWE Intern First-Round Questions: 20 Experiences
All Citadel interview questionsSee Citadel hiring insights
Saved
class Solution {
    public String[] reverseLines(String text) {
        // write your code here
    }
}
Checking account…