Popular Consecutive Flight Segments
At Ixigo, analyzing flight booking patterns is crucial for understanding demand, optimizing pricing strategies, and identifying potential anomalies. This problem focuses on extracting insights from raw booking data to pinpoint frequently traveled flight segments and observe their booking consistency. Your task is to process a stream of flight booking records to identify segments with high demand and understand how often they are booked consecutively, which can signal sudden surges or consistent popularity.
Your goal is to implement a function that processes a list of flight bookings and identifies popular segments, along with their maximum consecutive booking streaks.
bookings, where each element is a [originAirportCode, destinationAirportCode] pair, and an integer k.[origin, destination].k times or more in total. These are considered "popular" segments.bookings list to find the maximum number of times that segment appeared consecutively.segment: The [origin, destination] pair as a string[].maxConsecutiveBookings: The maximum consecutive count found for that segment.originAirportCode of the segment. If origins are identical, then sort by the destinationAirportCode.Function Signature:
function getPopularConsecutiveSegments(bookings: string[][], k: number): { segment: string[], maxConsecutiveBookings: number }[]
bookings: An array of arrays, where each inner array [origin, destination] represents a flight segment. origin and destination are 3-letter uppercase airport codes (e.g., "DEL", "BOM").k: An integer representing the minimum total booking count for a segment to be considered "popular".segment (a string[] representing [origin, destination]) and maxConsecutiveBookings (a number).bookings array should result in an empty returned array.k threshold, an empty array should be returned.maxConsecutiveBookings for a segment is 1 if it appears only once or never consecutively.["del", "bom"] is different from ["DEL", "BOM"], but per constraints, all will be uppercase).Examples
Example 1
Input:
bookings = [["DEL", "BOM"], ["BOM", "BLR"], ["DEL", "BOM"], ["DEL", "BOM"], ["BOM", "BLR"], ["DEL", "BOM"]], k = 3
Output:
[ { "segment": ["DEL", "BOM"], "maxConsecutiveBookings": 2 } ]
Note:
DEL-BOM: 4, BOM-BLR: 2.DEL-BOM.DEL-BOM: Bookings are ["DEL","BOM"], ["BOM","BLR"], ["DEL","BOM"], ["DEL","BOM"], ["BOM","BLR"], ["DEL","BOM"]. The longest streak for DEL-BOM is 2 (at indices 2 and 3).Example 2
Input:
bookings = [["NYC", "LAX"], ["LAX", "SFO"], ["NYC", "LAX"], ["NYC", "LAX"], ["LAX", "SFO"], ["LAX", "SFO"], ["LAX", "SFO"], ["NYC", "LAX"]], k = 3
Output:
[ { "segment": ["LAX", "SFO"], "maxConsecutiveBookings": 3 }, { "segment": ["NYC", "LAX"], "maxConsecutiveBookings": 2 } ]
Note:
NYC-LAX: 4, LAX-SFO: 4.LAX-SFO, NYC-LAX.NYC-LAX: The longest streak is 2 (at indices 2 and 3).LAX-SFO: The longest streak is 3 (at indices 4, 5, and 6).LAX-SFO comes before NYC-LAX alphabetically.Example 3
Input:
bookings = [["A", "B"], ["C", "D"], ["A", "B"]], k = 3
Output:
[]
Note:
A-B: 2, C-D: 1.k=3 threshold. The output is an empty array.Constraints
1 <= bookings.length <= 10^5bookings[i].length === 2 for all i.bookings[i][j] consists of exactly 3 uppercase English letters (e.g., "DEL", "BOM").1 <= k <= bookings.lengthbookings.length.k out of range).k threshold.Loading editor...