Top Five Stock Records in a Date Range
Updated
Problem statement
You are given a list of stock records. Each row in records is [name,value,date], and all three fields are strings. You are also given two dates, startDate and endDate. Keep only the records whose date falls in that range, then return the top five of them.
The range includes both end dates. Each stock name appears in exactly one record. Rank the records by value, read as an integer, with larger values first. If two values are equal, compare the names in case-sensitive ASCII order and put the smaller name first. Do not combine records or work out changes in value.
Each value is a nonnegative whole number written in decimal, with no leading zeros except for the value 0 itself. Each date is a valid calendar date written as zero-padded YYYY-MM-DD. Return at most five full rows in ranked order, and keep each row's three strings exactly as given. If fewer than five records are in range, return all of them. If none are, return an empty array.
Function
topFiveStockRecords(records: String[][], startDate: String, endDate: String) → String[][]Examples
Example 1
records = [["A","10","2021-07-01"],["B","30","2021-07-02"],["C","30","2021-07-03"],["D","20","2021-07-04"],["E","40","2021-07-05"],["F","50","2021-07-06"],["G","999","2021-08-01"]]startDate = "2021-07-01"endDate = "2021-07-31"return = [["F","50","2021-07-06"],["E","40","2021-07-05"],["B","30","2021-07-02"],["C","30","2021-07-03"],["D","20","2021-07-04"]]Record G is dated in August, after the range ends, so it is left out. The other six records are in range, so only the top five are returned. F and E have the two largest values, so they come first. B and C have the same value, and B comes before C by name. D comes next. A has the smallest value, so it is the one dropped.
Example 2
records = [["Beta","7","2021-07-15"],["Alpha","7","2021-07-15"],["Later","100","2021-07-16"]]startDate = "2021-07-15"endDate = "2021-07-15"return = [["Alpha","7","2021-07-15"],["Beta","7","2021-07-15"]]The range starts and ends on the same day, and that day counts. Alpha and Beta are both dated that day, so both are kept. Later is dated the next day, so it is left out. Alpha and Beta have the same value, so they are ordered by name, and Alpha comes before Beta.
Example 3
records = [["Old","100","2020-12-31"],["New","0","2022-01-01"]]startDate = "2021-01-01"endDate = "2021-12-31"return = []Old is dated the day before the range starts, and New is dated the day after it ends. Neither record is in range, so the answer is an empty array.
Constraints
0 <= records.length <= 300. Every row has exactly three strings.- Names are unique and case-sensitive. Each name has 1 to 20 ASCII letters or digits.
- Each value is a decimal integer from
0to1000000, written with no leading zeros. - Every record date,
startDateandendDateare valid YYYY-MM-DD dates from 2000-01-01 to 2099-12-31.startDateis never later thanendDate.
More Compass International Holdings Phone Screen questions
- Decode StringPHONE SCREEN · Seen Oct 2026
- Lowest Common Ancestors in a Parent DAGPHONE SCREEN · Seen Apr 2022
- Can One Dictionary Word Supply the Target Letters?PHONE SCREEN · Seen Apr 2022
- Greedy Grid Word Occurrences with No Reused CellsPHONE SCREEN · Seen Apr 2022
- Word Path Moving Only Right or DownPHONE SCREEN · Seen Apr 2022
- First Dictionary Word from Note LettersPHONE SCREEN · Seen Mar 2022
- Word Path Moving Only Up or RightPHONE SCREEN · Seen Feb 2022
- Wrap Words from Existing Text LinesPHONE SCREEN · Seen Apr 2021
- All Common Ancestors in a Parent GraphPHONE SCREEN · Seen Jan 2021
- Nodes With Zero or Three ParentsPHONE SCREEN · Seen Jan 2021
Similar questions at other companies
- Majority ElementByteDance · PHONE SCREENByteDance interview questions
- Recommend Films from Viewers Who Watched Everything You DidBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Total Years of Work ExperienceLinkedIn · PHONE SCREENLinkedIn interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Employee Count Median, Mean and ModeClay · PHONE SCREEN
- Squares of a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
- Assign CookiesAmazon · PHONE SCREEN
- Sort by Checking PermutationsMercor · PHONE SCREENMercor interview questions
- Meeting RoomsGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Parse and Sort Integer DataLegora · PHONE SCREEN