 # TCS NQT Bus Fare Coding Question

TCS NQT Bus Fare Coding Question

We at gradjobopenings.com provide free job alerts of freshers job drives. In this website we list on campus job openings for freshers and off campus job openings for freshers and also work from home job openings. This is the best website to apply for off campus drive in India. Visit our website for government job alerts and private job alerts. We also list free interview notes and study materials, one of the best interview study website.comfortable to face the interviews:

Problem Statement

A City Bus is a Ring Route Bus that runs in a circular fashion. That is, the Bus once started at Source Bus Stop, halts at each Bus Stop in its Route and at the end, it reaches the Source Bus stop again. If there are n stops and if the bus starts at stop 1, then after the nth stop, the next stop in the Route will always be stop 1 always. If there are n stops, there will be n paths. One path connects 2 stops. Distances (in meters) for all paths in Ring Route is given in array Path[] as below.

Path = [800, 600, 750, 900, 1400, 1200, 1100, 1500]

Fare is determined based on the distance covered from source to destination stop as Distance d between Input Source and Destination Stops can be measured by looking at values in array Path[] and fare can be calculated as per the following criteria:

If d = 1000 meters, then fare= 5 INR – (the calculated fare containing any fraction value should be ceiled. For example, for distance 900, when fare initially calculated is 4.5 which must be ceiled to 5). The path is circular. Value at each index indicates distance till current stop from the previous one.

And each index positions can be mapped with values at the same index in Busstops[] array, which is a string array holding abbreviation of names for all stops as:

Given, n=8, where n is the number of total Bus Stops. Bus Stops = [“TH”, “GA”, “IC”, “HA”, “TE”, “LU”, “NI”, “CA”]. Write a code with a function getFare(String Source, String Destination) which takes Input as a source and destination stops (in the format containing first 2 characters of Name of the Bus stop) and calculate and return travel fare.

Example 1:

Input Values

ca

Ca

Output Values

INVALID INPUT

Example 2:

Input Values

NI

HA

Output Values

23

Note: Input and Output should be in the format given in the example.

Input should not be case sensitive and output should be in the format <FLOAT> INR.

Algorithm

“`

1. Initialise bus_stops array with: {“TH”, “GA”, “IC”, “HA”, “TE”, “LU”, “NI”, “CA”}

2. Initialise distance array with: {800, 600, 750, 900, 1400, 1200, 1100, 1500}

4. If either source or distance is not of length 2, do

Print “INVALID INPUT”

exit

5. Replace all characters in source and destination to their uppercase counterparts

6. If source == destination, do

Print “INVALID INPUT”

exit

7. Initialise src_index and dist_travel to 0

8. For i in 0 to (length of bus_stops array) – 1 inclusive, do:

if src == bus_stops[i], do

src_index = i + 1

while src_index != i, do

dist_travel = dist_travel + distance[src_index]

if destination == bus_stops[src_index], do

break

src_index = (src_index + 1) % (length of bus_stops array)

break

9. fare = dist_travel * 0.005 // Rs 5 for 1000m

10. Print fare

Program In C

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

char bus_stops = {"TH", "GA", "IC", "HA", "TE", "LU", "NI", "CA"};
int distance = {800, 600, 750, 900, 1400, 1200, 1100, 1500};

double getFare(char src[], char dest[])
{
if ( src > 190 )
src -= 32;
if ( src > 190 )
src -= 32;
if ( dest > 190 )
dest -= 32;
if ( dest > 190 )
dest -= 32;
if (strncmp(src, dest, 2) == 0 )
{
printf("INVALID INPUT");
return 0;
}
int src_index, dist_travel = 0;
for (int i = 0 ; i < 8 ; i++)
{
if (strncmp(src, bus_stops[i], 2) == 0)
{
src_index = i + 1;
while (src_index != i)
{
dist_travel += distance[src_index];
if (strncmp(dest, bus_stops[src_index], 2) == 0)
break;
src_index = (src_index + 1) % 8;
}
break;
}
}
return ceil(dist_travel * 5.0/1000);
}

int main()
{
char src, dest;
scanf("%s%s", src, dest);
if (strlen(src) != 2 || strlen(dest) != 2)
{
printf("INVALID INPUT");
return 0;
}
printf("%.1lf", getFare(src, dest));
return 0;
}
``````

Program In C++

``````#include<bits/stdc++.h>
#include <vector>
#include <cmath>

using std::cin;
using std::cout;
using std::vector;
using std::string;
using std::ceil;

int main() {
vector <string> bus_stops {"TH", "GA", "IC", "HA", "TE", "LU", "NI", "CA"};
vector <int> distance {800, 600, 750, 900, 1400, 1200, 1100, 1500};
string src, dest;
cin >> src >> dest;
if ( src.size() != 2 || dest.size() != 2 )
{
cout << "INVALID INPUT";
return 0;
}
transform ( src.begin(), src.end(), src.begin(), ::toupper );
transform ( dest.begin(), dest.end(), dest.begin(), ::toupper );
if (src == dest)
{
cout << "INVALID INPUT";
return 0;
}
int src_index {0}, dist_travel {0}, i;
for ( i = 0 ; src != bus_stops.at(i) ; i++);
src_index = i + 1;
while ( src_index != i )
{
dist_travel += distance.at(src_index);
if ( dest == bus_stops.at(src_index) )
break;
src_index = ( src_index + 1 ) % 8;
}
cout << ceil (dist_travel * 5.0 / 1000);
return 0;
}
``````

Program In Java

``````import java.util.Scanner;
class Main
{
static String bus_stops[] = {"TH", "GA", "IC", "HA", "TE", "LU", "NI", "CA"};
static int distance[] = {800, 600, 750, 900, 1400, 1200, 1100, 1500};

static double getFare(String src, String dest)
{
if (src.length() != 2 || dest.length() != 2 || src.equals(dest))
{
System.out.print("INVALID INPUT");
System.exit(0);
}
int src_index, dist_travel = 0;
for (int i = 0 ; i < 8 ; i++)
{
if (src.equals(bus_stops[i]))
{
src_index = i + 1;
while (src_index != i)
{
dist_travel += distance[src_index];
if (dest.equals(bus_stops[src_index]))
break;
src_index = (src_index + 1) % 8;
}
break;
}
}
return Math.ceil(dist_travel * 5.0/1000);
}

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String src = in.next().toUpperCase();
String dest = in.next().toUpperCase();
System.out.print(Main.getFare(src, dest));
}
}
``````

Program In Python

``````bus_stops = ["TH", "GA", "IC", "HA", "TE", "LU", "NI", "CA"]
distance = [800, 600, 750, 900, 1400, 1200, 1100, 1500]

def getFare(src, dest):
dist_travel = 0
for stop in bus_stops:
if src == stop:
i = bus_stops.index(src)
src_index = i + 1
while src_index != i:
dist_travel += distance[src_index]
if dest == bus_stops[src_index]:
break
src_index = (src_index + 1) % 8
break
import math
return math.ceil(dist_travel * 5.0/1000)

if __name__ == "__main__":
src = input().upper()
dest = input().upper()
if len(src) != 2 or len(dest) != 2 or src == dest:
print ("INVALID INPUT")
exit()
print (float(getFare(src, dest)))
``````