Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
well tell me tell u clearly with an example.. Lets take a single straight line and consider it to be road.
---------------------------------------------------------------------------------------------------> road
1 2 1 1 1 2 1 1 1 2 1 1 1 1 1 2 1 3(terminate)
(sec) (car)
so as drawn above 1 represents each second .. 2 represents the car which passes in the road. and 3 represents terminate. (may be consider it as end point.. he did not say as end point but just said 3 means terminate and if 3 is clicked no one is allowed to pass in the road and stop counting the seconds as well and exit the program) . So lets say we have detectors and microprocessors in road which controls the signal and detects the cars which are passing in the road.. we need to now cal each sec and print the total number of seconds , count the no of cars which passed in the road and therefore print out the no of cars passed in the road. and finally max time difference between the two cars which passed in the road. (so in this case the max time difference would be b/w the last two cars (last 2s(2 cars)) . i was given a method called int Getevent(); . I was asked to write the code within single loop which is inside this getevent() method.. ( java prog lang) example which i have drawn above is just for u to understand the scenario. there can be many no of cars which can pass the road like this.. so we need print out three things . i am summarizing it again.
i) print the no of seconds
ii) print the no cars passed in the road.
iii) print the maximum time difference between two cars which passed in the road.
So why is this hard?
int seconds = 0, cars = 0, lastCar = Integer.MAX_VALUE, maxGap = 0;
foreach (value in sequence)
{
if (value == 1) { seconds++; }
if (value == 2) {
cars++;
if (seconds - lastCarTime > maxGap) {
maxGap = seconds - lastCarTime; }
lastCarTime = seconds;
}
if (value == 3) { break; }
}
#include<iostream>
using namespace std;
main()
{
int i,t=0,c=0,d=0,max=0;
while (1)
{
cin>>i;
if (i==1)
{
t++;
d++;
}
else if (i==2)
{
c++;
if(max<d)
{
max=d;
}
d=0;
}
else if (i==3)
{
cout<<"total tym"<<" "<<t<<"\n";
cout<<"total car"<<" "<<c<<"\n";
cout<<"max tym diff"<<" "<<max<<"\n";
break;
}
}
cin>>i;
}
#include<iostream>
using namespace std;
main()
{
int i,t=0,c=0,d=0,max=0;
while (1)
{
cin>>i;
if (i==1)
{
t++;
d++;
}
else if (i==2)
{
c++;
if(max<d)
{
max=d;
}
d=0;
}
else if (i==3)
{
cout<<"total tym"<<" "<<t<<"\n";
cout<<"total car"<<" "<<c<<"\n";
cout<<"max tym diff"<<" "<<max<<"\n";
break;
}
}
cin>>i;
}
You're not being clear enough about the question for me to take a stab at it.
- eugene.yarovoi April 14, 2012