How to solve the Service Lane Challenge from Hackerrank.(Javascript).

Godwin Nwachukwu
4 min readMar 27, 2020

--

https://unsplash.com/photos/FcLq69V7Rsc
Photo by John Lockwood

Hackerrank challenges are quite easy. One recurring problem is fully understanding the problem statement.

The first step to problem-solving as we all know is to first identify the problem.

Taking a look at the challenge(here is the link):

We are given two parameters in the serviceLane function description: n and cases.

Sample Input

8 5 
2 3 1 2 3 2 3 3
0 3
4 6
6 7
3 5
0 7

which translates to:

n, t.  n = width.length = 8,  t = cases.length = 5.
width = [2, 3, 1, 2, 3, 2, 3, 3].
t lines = cases = [[0, 3],[4, 6],[6, 7],[3, 5], [0, 7]].
i j i j i j i j i j

Each individual array in cases array represents entry and exit points called i and j also called test cases.

But in the serviceLane function, we just have only two parameters: n(width.length) and cases.

Explanation:

Using i and j as indexes for a range of numbers from the width array,

[0,3] from the cases array gives us this subset of width array: [2,3,1,2]
[4,6] from the cases array gives us this subset of width array: [3,2,3]
[6,7] from the cases array gives us this subset of width array: [3,3]
[3,5] from the cases array gives us this subset of width array : [2,3,2]
[0,7] from the cases array gives us this subset of width array: [2,3,1,2,3,2,3,3]

In solving algorithmic problems, we always have to look for a pattern.

The pattern here is that the maximum size vehicle going through each of these road segments is the minimum number in each of the width array subsets. The function must return the array of minimum size vehicles from each of the subsets.

Given my solution:

// Complete the serviceLane function below.
function serviceLane(n, cases) {
//1.Get an array of arrays of road segments slicing at the entry(i) and exit(j) points.(subsets of width array).
let slicedWidths = [];
for(let i = 0; i < t; i++){
let unit = cases[i];
let widthSlice = width.slice(unit[0], unit[1]+1);
slicedWidths.push(widthSlice);
}
//2.Get an array of Maximum car widths that can pass through each road segment.
let maxWidthsOfVehicle = [];
for (let g = 0; g < slicedWidths.length; g++){
maxWidthsOfVehicle.push(Math.min(...slicedWidths[g]));
}
return maxWidthsOfVehicle;}//reference error: width is undefined, t is undefined

It returns a reference error that says width is undefined.

This did not work until I looked through Hackerrank’s code that executes the function. and found this:

function main() {const ws = fs.createWriteStream(process.env.OUTPUT_PATH);const nt = readLine().split(‘ ‘);const n = parseInt(nt[0], 10);const t = parseInt(nt[1], 10);const width = readLine().split(‘ ‘).map(widthTemp => parseInt(widthTemp, 10));let cases = Array(t);for (let i = 0; i < t; i++) {cases[i] = readLine().split(‘ ‘).map(casesTemp => parseInt(casesTemp, 10));}let result = serviceLane(n, cases);ws.write(result.join(“\n”) + “\n”);ws.end();}

The width parameter was deliberately not included as a parameter and argument in the function call and function declaration. This is just a trick.

Looking at the result variable in the Hackerrank code(from the last code snippet) that executes the function, we have:

let result = serviceLane(n, cases);

so I corrected the result variable:

let result = serviceLane(n, t, width, cases);

and also changed it in the function declaration:

function serviceLane(n, t, width, cases) {}

and it did the magic:

// Complete the serviceLane function below.
function serviceLane(n, t, width, cases) {
//1.Get an array of arrays of road segments slicing at the entry(i) and exit(j) points.(subsets of width array).
let slicedWidths = [];
for(let i = 0; i < t; i++){
let unit = cases[i];
let widthSlice = width.slice(unit[0], unit[1]+1);
slicedWidths.push(widthSlice);
}
//2.Get an array of maximum car widths that can pass through each road segment.
let maxWidthsOfVehicle = [];
for (let g = 0; g < slicedWidths.length; g++){
maxWidthsOfVehicle.push(Math.min(...slicedWidths[g]));
}
return maxWidthsOfVehicle;}// [1,2,3,2,1]

Sometimes you would have to look through the Hackerrank code that executes the function after many tries and reviews. It’s part of the challenge.

Refactoring the code, (neat and efficient):

In three(3) code lines,

// Complete the serviceLane function below.
function serviceLane(n, t, width, cases) {
//1.Get an array of arrays of road segments slicing at the entry(i) and exit(j) points.(subsets of width array).let slicedWidths = cases.map(s=>width.slice(s[0], s[1]+1));//2.Get an array of maximum car widths that can pass through each road segment.let maxWidthsOfVehicle = slicedWidths.map(b=>Math.min(...b));return maxWidthsOfVehicle;}// [1,2,3,2,1]

For more understanding of .map and .filter inbuilt es6+ javascript methods, Check out these links:

I hope you really understood this. Let me know your thoughts and comments in the review section

Cheers.

--

--