HackerRank Service Lane Solution

Hello Programmers, In this post, you will learn how to solve HackerRank Service Lane Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

HackerRank Service Lane Solution
HackerRank Service Lane Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

HackerRank Service Lane


A driver is driving on the freeway. The check engine light of his vehicle is on, and the driver wants to get service immediately. Luckily, a service lane runs parallel to the highway. It varies in width along its length.

Paradise Highway

You will be given an array of widths at points along the road (indices), then a list of the indices of entry and exit points. Considering each entry and exit point pair, calculate the maximum size vehicle that can travel that segment of the service lane safely.


n = 4
width = [2, 3, 2, 1]
cases = [[1, 2], [2, 4]]

If the entry index, i = 1 and the exit, j = 2, there are two segment widths of 2 and 3 respectively. The widest vehicle that can fit through both is 2. If i = 2 and j = 4, the widths are [3, 2, 1] which limits vehicle width to 1.

Function Description

Complete the serviceLane function in the editor below.

serviceLane has the following parameter(s):

  • int n: the size of the width array
  • int cases[t][2]: each element contains the starting and ending indices for a segment to consider, inclusive


  • int[t]: the maximum width vehicle that can pass through each segment of the service lane described

Input Format

The first line of input contains two integers, n and t, where n denotes the number of width measurements and t, the number of test cases. The next line has n space-separated integers which represent the array width.

The next t lines contain two integers, i and j, where i is the start index and j is the end index of the segment to check.


  • 2 <= n <= 100000
  • 1 <= t <= 1000
  • 0 <= i < j < n
  • 2 <= j – i + 1 <= min(n, 1000)
  • 1 <= width[k] <= 3, where 0 <= k < n

Sample Input

STDIN Function
—– ——–
8 5 n = 8, t = 5
2 3 1 2 3 2 3 3 width = [2, 3, 1, 2, 3, 2, 3, 3]
0 3 cases = [[0, 3], [4, 6], [6, 7], [3, 5], [0, 7]]
4 6
6 7
3 5
0 7

Sample Output



Below is the representation of the lane:

|HIGHWAY|Lane| -> Width

0: | |–| 2
1: | |—| 3
2: | |-| 1
3: | |–| 2
4: | |—| 3
5: | |–| 2
6: | |—| 3
7: | |—| 3

  1. (0, 3): From index 0 through 3 we have widths 2, 3, 1 and 2. Nothing wider than 1 can pass all segments.
  2. (4, 6): From index 4 through 6 we have width 3, 2 and 3. Nothing wider than 2 can pass all segments.
  3. (6, 7):  3, 3 -> 3.
  4. (3, 5)2, 3, 2 >2
  5. (0, 7)2, 3, 1, 2, 3, 2, 3, 3 -> 1.

HackerRank Service Lane Solution

Service Lane Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int i,n,j,t,min;
    int* arr=(int*)malloc(n*sizeof(int));
    int p,q;
    return 0;

Service Lane Solution in Cpp

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int n, t;
    cin >> n >> t;
    int width[n];
    for(int i = 0; i < n; i++) cin >> width[i];
    for(int qq = 0; qq < t; qq++){
        int i, j;
        cin >> i >> j;
        int m = 3;
        for(int k = i; k <= j; k++){
            m = min(m, width[k]);
        cout << m << endl;
    return 0;

Service Lane Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] rags) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw = new PrintWriter(System.out);
		StringTokenizer st = new StringTokenizer(br.readLine());
		int N = Integer.parseInt(st.nextToken());
		int t = Integer.parseInt(st.nextToken());
		int[]r = new int[N];
		st = new StringTokenizer(br.readLine());
		for(int i=0;i<N;i++) {
			r[i] = Integer.parseInt(st.nextToken());
		while (t-- > 0) {
			st = new StringTokenizer(br.readLine());
			int i = Integer.parseInt(st.nextToken());
			int j = Integer.parseInt(st.nextToken());
			int mini = 3;
			for(int k=i;k<=j;k++) {
				mini = Math.min(mini, r[k]);

Service Lane Solution in Python

import math

n, t = map(int, raw_input().split())

width = map(int, raw_input().split())

for i in range(t):
	x,y = map(int, raw_input().split())
	minW = min(width[x:(y+1)])
	print minW

Service Lane Solution using JavaScript

'use strict';

function processData(input) {
    var parse_fun = function (s) { return parseInt(s, 10); };

    var lines = input.split('\n');
    var params = lines.shift().split(' ').splice(0, 2).map(parse_fun);
    var N = params[0];
    var T = params[1];

    var data = lines.shift().split(' ').splice(0, N).map(parse_fun);

    var res = [];
    for (var t = 0; t < T; t++) {
        var p = lines[t].split(' ').splice(0, 2).map(parse_fun);
        var i = p[0];
        var j = p[1];
        var m = data[i];
        for (var k = i + 1; k <= j; k++) {
            if (data[k] < m) { m = data[k]; }

var _input = "";
process.stdin.on("data", function (input) { _input += input; });
process.stdin.on("end", function () { processData(_input); });

Service Lane Solution in Scala

object Solution {

    def main(args: Array[String]) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution
    val N_T = readLine.split(' ').map(_.toInt)
    val arr = readLine.split(' ').map(_.toInt)
    val min = (start: Int, end: Int) =>  {
      var minimum = arr(start)
      for (i <- start to end) {
    	  if(arr(i) < minimum)
    	    minimum = arr(i) 
    for(i <- 0 until N_T(1)){
      val S_E = readLine.split(' ').map(_.toInt)
      println(min(S_E(0) , S_E(1)))

Service Lane Solution in Pascal

	N, T : longint;
    i, j, k, l, m, min : longint;
    width: array [0..100000] of integer;
    a: array [1..1000] of integer;

    for k:=1 to N do
    for l:=1 to T do
    	for m:=i to j do
        	if width[m]<min then
    for l:=1 to T do

Disclaimer: This problem (Service Lane) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.


1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume. 

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite. 

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe. 

Sharing Is Caring

Leave a Comment