This is a premium problem. We're working on making it available for free soon.
Use these hints if you're stuck. Try solving on your own first.
<p>Sort the elements by distance. In case of a tie, sort them by the index of the worker. After that, if there are still ties, sort them by the index of the bike.</p> <p>Can you do this in less than O(nlogn) time, where n is the total number of pairs between workers and bikes?</p>
Loop the sorted elements and match each pair of worker and bike if the given worker and bike where not used.
Solutions for this premium problem will be available for free soon.
Browse Free ProblemsWatch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, problems similar to Campus Bikes appear in technical interviews at major tech companies. They test understanding of greedy strategies, sorting, and efficient pair processing.
A common optimal strategy is to compute all worker–bike pairs with their Manhattan distances and sort them. Then greedily assign bikes starting from the smallest distance while ensuring each worker and bike is used only once.
Manhattan distance measures grid-based movement using |x1-x2| + |y1-y2|. Since workers and bikes are placed on a grid, this metric correctly represents the distance required to move between two points.
Arrays or lists are typically used to store worker–bike pairs along with their computed distances. Sorting structures or bucket arrays grouped by distance help efficiently process the pairs in increasing order.