Last X Lines of a File
Updated
Problem statement
Given file contents text and a nonnegative integer x, return the last at most x lines.
The newline character \n separates lines. Consecutive newlines create empty lines. A final newline terminates the preceding line and does not create an additional trailing empty line. Empty text contains zero lines. Preserve each returned line exactly.
Function
lastFileLines(text: String, x: int) → String[]Examples
Example 1
text = "a\nb\nc"x = 2return = ["b","c"]The final two of the three lines are b and c.
Example 2
text = "a\nb\n"x = 2return = ["a","b"]The terminal newline closes line b without adding another line.
Example 3
text = ""x = 5return = []An empty file has no lines.
Constraints
0 <= text.length <= 1000000.0 <= x <= 1000000.textmay contain any characters except carriage return.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Run-Length Encoding and DecodingAirbnb · PHONE SCREENAirbnb interview questions
- Check if a String Is a PalindromeCisco · PHONE SCREENCisco interview questions
- Halfway Course in a Single TrackCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Reverse a StringAmazon · PHONE SCREEN
- Reverse a String ManuallyNewgen Software · PHONE SCREEN
- Reverse a String with Two PointersSnowflake · PHONE SCREENSnowflake interview questions
- Palindrome with One DeletionWhatNot · PHONE SCREENWhatNot interview questions
- Valid Word AbbreviationMeta · PHONE SCREENMeta interview questions
- Reverse a StringPersistent Systems · PHONE SCREEN
- Minimum Changes for Palindromic BlocksCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions