Skip to content

Instantly share code, notes, and snippets.

@el1s7
Last active April 20, 2021 19:53
Show Gist options
  • Save el1s7/513b7dae6a5244132045c5b104f17e41 to your computer and use it in GitHub Desktop.
Save el1s7/513b7dae6a5244132045c5b104f17e41 to your computer and use it in GitHub Desktop.
Uber Coding Challange
/*
I applied for a engineering position at Uber, I had to do some coding challanges as a part of interview
I failed solving any challange in 3 hours time limit, felt a little dumb tbh
After a few days I tried solving one of the challanges
Finished it in about 20 minutes
The Challange:
There is a list of available Taxis and the time they take to finish one trip in minutes.
I.e [2,5,8] -> There are 3 Taxis, the first one takes 2 minues, the second 5 minutes and the third one 8 minutes.
The Taxis can do parallel trips.
Your task is to find the fastest amount of time it would take for the taxis to complete X amount of trips.
For example:
If we have these taxis: [1,2]
And we want to complete: 3 Trips
The fastest time would be: 2 minutes
Since the first Taxi can do 1x2 (Two trips in 2 minutes) and the second taxi will do 2x1 (One trip in 2 minutes)
Total 3 Trips in 2 minutes
Here is my code for that:
*/
function getTaxiTime(n, taxis) {
var taxis = taxis.sort((a, b) => a - b);
var calculate = function (n, taxis, endIndex) {
for (var i = 0; i < endIndex; i++) {
var currentTaxis = taxis.slice(0, i + 1);
var currentTime = Math.max(...currentTaxis);
var totalTrips = currentTaxis.reduce(
(o, v, i) => o + Math.floor(currentTime / v),
0
);
if (totalTrips >= n) {
return [n, currentTime, totalTrips];
}
if (i + 1 == endIndex) {
var [_, extraCurrentTime, extraTotalTrips] = calculate(
n - totalTrips,
taxis,
endIndex
);
return [
n,
currentTime + extraCurrentTime,
totalTrips + extraTotalTrips
];
}
}
};
//var [trips, time, max] = calculate(n, taxis, taxis.length);
var bestTime = taxis
.map((taxi, i) => calculate(n, taxis, i + 1))
.sort((a, b) => a[1] - b[1]);
var [trips, time, max] = bestTime[0];
console.log("Trips: ", trips);
console.log("Best Time: ", time);
console.log("Max Capacity: ", max);
}
console.clear();
getTaxiTime(15, [2,1,50,60]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment