## Interview Question

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

I assume each bus stop lies on at least 1 route. So let's say we have a table like RouteTable(BusStopName,RouteNumber). So if a new bus stop lying on n number of routes is added, we add n rows to this table.

1.For finding all the bus stops on a route,
select BusStopName from RouteTable where RouteNumber=?

2.For finding all the routes between bus stops s and d

select RouteNumber from RouteTable where BusStopName = s
INTERSECTION
select RouteNumber from RouteTable where BusStopName = d

If the above query returns nothing, there is no connecting direct route. In such a case,
1.you need to find out all the bus stops which lie on the routes having s.
2.Similarly find out all the bus stops which lie on the routes having d.
3.Find the common bus stops of steps 1 and 2 above along with their routes numbers for s and d.
4.The result of 3 would give you the change bus stop and the 2 routes which you would follow one after another to reach from s to d.

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

Thanks your solution will work. Step 1: as per ur defination of table we will get all bus_stop_names on a specific route but what if we also want them in order bus traverse them. I think one solution for this will be, add one more column having sequence_no like source as 1, destination as N and in between bus_stops will get corresponding number.

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

``````I think your solution with work for the first part.
In the second part you are assuming that, there is a single intermediate (transfer) stop. Your solution won't work if there are two or more transfer points.``````

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

The problem, as I understand, can be solved through using a graph structure.
If you have to use the Relational Database, you could create many-to-many relations between nodes(bus stop) and edges(with weight). Retrieve them into a standard structure (Directed graph) and then you could use standard algorithms (or methods) on the structure.

You could also use Neo4j (the Graph Database). It is easily expandable!

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.