The busiest pair of corners
- O(n) · Medium
- Full plan
- Python
- JavaScript
- dictionaries
- lists
- comparisons
Problem
Ofelia runs the taxi stand in her neighborhood and writes down every ride: from which corner to which corner. She wants to know which two corners ask for each other the most, so she can leave a taxi waiting there.
Write a function that takes trips, a list where each trip is ["from", "to"], two different corners written in lowercase, with no accents and no spaces. A ride there and a ride back between the same two corners count for the same pair. Return the busiest pair as a list with its two corners in alphabetical order.
With these five rides: market to station, station to market, square to hospital, market to square, and station to market, the market and station pair was asked for three times and no other more than once, so you return ["market", "station"].
If two or more pairs tie, return the one that comes first in the alphabet: compare the corner that comes first in each pair and, if it is the same one, the other. If there are no trips, there is no pair and you return an empty list.
Examples
The example
[["market", "station"], ["station", "market"], ["square", "hospital"], ["market", "square"], ["station", "market"]] → ["market", "station"]
A tie, the first in the alphabet wins
[["clocktower", "nursery"], ["square", "bridge"], ["nursery", "clocktower"], ["bridge", "square"]] → ["bridge", "square"]
A single ride
[["station", "park"]] → ["park", "station"]
No trips
[] → []
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def busiest_pair(trips):
passJavaScript
function busiestPair(trips) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.