## Samsung Interview Question for SDE1s

Country: India
Interview Type: Written Test

Comment hidden because of low score. Click to expand.
1
of 1 vote

There are N cars parked in a row in a parking lot of the newly  constructed club. as it is demonstrated in the picture below.

There is a gasoline and diesel fueling station installed.at the left and right side of the park.
An automatic fueling robot carries the fuel from station and fill up the parked car with fuel. The cars are divided into 2 types depending on whether it is a gasoline or diesel car.
1 is denoted as gasoline cars and 2 is denoted as diesel cars.
The automatic robot will be used to provide a cost free fueling service which is filling up  all cars with 1 litre of each corresponding fuel.
The robot will move in between the 2 fuelling stations as below :   1) The robot carries 2 litre of gasoline at the gasoline station and starts moving from there.  2) The robot can fill up the cars of the same type of gas it carries 1 litre each.  3) The robot can go back to the fuelling station at any time, Independent from the current amount of fuel it carries.  4) When the robot arrives at the fuelling station, it gets 2 litre of supply of the corresponding fuel.(If the robot has some remaining fuel it will be discarded).
5) There is an equal distance of 1 between each fueling station and the cars.

The fuel type of N Cars parked in the parking lot will be given.
Find the minimum moving distance of the automated fueling robot after it has filled up all the cars with 1 litre  of fuel each.

Time limit: C/C++/Java: 3 seconds.
Test cases: 50
2<=N<=8
I/P format:
2  Total number of test cases
5  N(Number of cars between gasoline and Diesel stations)
1 2 1 2 1(1 Gasoline car, 2Diesel cars)
5
2 1 1 2 1

O/P:
#1 12
#2 14 Example 1)  Given the total number of cars N = 5 and the order of the parked cars such as G - D - G - D - G (PS: G-> Gasoline, D->Diesel)
the process of finding the minimum moving distance for fueling the car is as follows :

#include<stdio.h>
#include<string.h>
#define MAX 9
int arr[MAX];
int visited[MAX];
int tdistance=99999;
void calc_min(int n,int distance,int rem,int pindex,int cars,int ins, int g_or_d)
{
int i;
if(cars==n)
{
if(tdistance>distance)
{
tdistance=distance;
}
return;
}
if(rem<=0) return;
if(ins==0 && g_or_d==0)
{
int k;
for(k=1;k<=n;k++)
{
if(!visited[k] &&arr[k]==1)
{
visited[k]=1;
//go to next gas car
calc_min(n,distance+(k>pindex ? k-pindex:pindex-k),rem-1,k,cars+1,0,0);
//go to gas station
calc_min(n,distance+(k>pindex ? k-pindex:pindex-k),2,k,cars+1,1,0);
//go to diesel station
calc_min(n,distance+(k>pindex ? k-pindex:pindex-k),2,k,cars+1,2,0);
visited[k]=0;
}
}
}
if(ins==0 && g_or_d==1)
{
int k;
for(k=n;k>=1;k--)
{
if(!visited[k] && arr[k]==2)
{
visited[k]=1;
//go to next gas car
calc_min(n,distance+(k>pindex ? k-pindex:pindex-k),rem-1,k,cars+1,0,1);
//go to gas station
calc_min(n,distance+(k>pindex ? k-pindex:pindex-k),2,k,cars+1,1,1);
//go to diesel station
calc_min(n,distance+(k>pindex ? k-pindex:pindex-k),2,k,cars+1,2,1);
visited[k]=0;
}
}
}
if(ins==1)
{
//fill gas and recall
calc_min(n,distance+(pindex-0),2,0,cars,0,0);
}
if(ins==2)
{
//fill diesel and recall
calc_min(n,distance+((n+1)-pindex),2,n+1,cars,0,1);
}

}
int main(void)
{
int T, test_case;
int n;
int i;
freopen("inp.txt", "r", stdin);
setbuf(stdout, NULL);
scanf("%d", &T);
for(test_case = 0; test_case < T; test_case++)
{
scanf("%d", &n);
for(i=1;i<=n;i++)
{
scanf("%d", &arr[i]);
}
calc_min(n,0,2,0,0,0,0);
if( tdistance==99999)
{
//May be all cars are diesel cars, go to diesel bunk and fuel it from there
calc_min(n,n+1,2,n+1,0,0,1);
}
printf("Case #%d %d\n", test_case+1,tdistance);
tdistance=99999;
}
getch();
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

I think you forgot to mention the question? you have just described the conditions.

Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Shiva, Thanks for sharing the question. Looks like you forgot to mention the question asked in this problem. you have nicely covered all the condition but I could not find the question we need to compute.

Comment hidden because of low score. Click to expand.
0
of 0 vote

Lol where is the question ??

Comment hidden because of low score. Click to expand.
0
of 0 vote

here is the question
https :// ideone.com /kdtB0U

Comment hidden because of low score. Click to expand.
0
of 0 vote

here is the question
(ideone.com/kdtB0U)

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.