Find Maximum Eastbound City Visits and Parse CSV
Company: Upstart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: The first problem evaluates algorithmic reasoning about ordering constraints and subsequence selection, testing skills in sequence algorithms, multidimensional comparison (geographic coordinates combined with lexicographic ordering) within the coding and algorithms domain; it is commonly asked to gauge the ability to model ordering constraints and implement efficient algorithmic solutions, emphasizing practical implementation over purely conceptual theory. The second problem evaluates string parsing and tokenization competencies, including handling quoted fields, delimiter rules, and preservation or removal of quote characters within the parsing/data-processing domain; it is commonly asked to assess robustness handling of input-format edge cases and focuses on practical parsing implementation.
Part 1: Maximum Eastbound City Visits
Constraints
- 0 <= len(cities) == len(x) <= 100000
- City names are non-empty strings when present.
- The total length of all city names is at most 1000000.
- -10^9 <= x[i] <= 10^9
- Duplicate city names and duplicate coordinates may appear; both the coordinate and name comparisons are strict.
Examples
Input: (["Tucson", "Albany", "Smith", "Westford", "Berkeley"], [182, 93, 114, 1421, 50])
Expected Output: 4
Explanation: One optimal route is Albany -> Smith -> Tucson -> Westford.
Input: ([], [])
Expected Output: 0
Explanation: There are no cities to visit.
Input: (["Only"], [10])
Expected Output: 1
Explanation: A single city can always be visited.
Input: (["A", "B", "C"], [1, 1, 2])
Expected Output: 2
Explanation: A and B cannot both be used in order because their coordinates are equal. The best route has length 2, such as A -> C.
Input: (["C", "B", "A"], [1, 2, 3])
Expected Output: 1
Explanation: Although coordinates increase, city names decrease, so no two-city route is valid.
Input: (["A", "A", "B", "C"], [1, 2, 3, 4])
Expected Output: 3
Explanation: The traveler cannot move from A to A because names must be strictly greater. One best route is A -> B -> C.
Hints
- Think of each city as a point with two ordered properties: coordinate and lexicographic name.
- After sorting by coordinate, use a data structure to quickly find the best previous route ending with a smaller city name. Be careful not to chain cities with the same coordinate.
Part 2: Parse a Comma-Separated String with Quotes
Constraints
- 0 <= len(s) <= 100000
- The input string contains ordinary characters, commas, single quotes, and double quotes.
- Quoted sections are balanced and valid.
- There are no escape sequences; a matching quote ends the current quoted section.
- Empty fields are allowed, including consecutive commas and a trailing comma.
Examples
Input: ("Hello,'Hi \"to\" you'",)
Expected Output: ["Hello", "Hi \"to\" you"]
Explanation: The comma after Hello splits the fields, but the double quotes inside the single-quoted field are preserved.
Input: ("a,\"b,c\",d",)
Expected Output: ["a", "b,c", "d"]
Explanation: The comma inside the double-quoted field does not split the field.
Input: ("'x,y',\"z's\",plain",)
Expected Output: ["x,y", "z's", "plain"]
Explanation: Single quotes delimit the first field, double quotes delimit the second field, and the apostrophe inside the double-quoted field is preserved.
Input: ("",)
Expected Output: []
Explanation: By definition for this problem, an empty input string represents no fields.
Input: ("one,,\"\",'a,b',tail,",)
Expected Output: ["one", "", "", "a,b", "tail", ""]
Explanation: The parser preserves empty fields, handles an empty quoted field, keeps the comma inside the single-quoted field, and returns an empty final field after the trailing comma.
Hints
- Scan the string from left to right while tracking whether you are currently inside a quoted section.
- A comma splits the current field only when you are not inside quotes.