Microsoft Interview Question
Senior Software Development EngineersCountry: United States
Interview Type: In-Person
Didn't understand the last part with Stack, is the idea is the requesting aircrafts ( for terminal ) goes into the stack (Push) ? if so the last requesting aircraft gets the terminal.
The Stack consists of the list of runways. I will have 2 stacks one for long runways and other for short runways. Whenever an aircraft comes we will pop a runway from the stack depending upon the size requirement and when the aircraft leaves runway we will push into a stack
This seems to me a concurrent application
Something like this:
class Runway implements Runnable{
private final BlockingQueue<Aircraft> requests;
public Runway(){
requests = new LinkedBlockingQueue<Aircraft>();
}
public void requestLanding(Aircraft a){
try {
requests.put(a);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void dispatchRequest() {
Aircraft a;
try {
a = requests.take();
a.land();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run(){
while(true) {
dispatchRequest();
}
}
}
class Aircraft{
public void land(){
Thread.sleep(2000);
}
}
public class Terminal
{
private class WaitingRunwayRequestor
{
public IRunwayRequestor runwayRequestor;
public int minLength;
}
public List<Runway> AvailableRunways{get;set;}
public List<WaitingRunwayRequestor> WaitingRunwayRequestors { get; set; }
public Terminal()
{
AvailableRunways = new List<Runway>();
WaitingRunwayRequestors = new List<WaitingRunwayRequestor>();
}
public void RequestRunway(IRunwayRequestor runwayRequestor, int minLength)
{
Runway allocatedRunway=null;
foreach (Runway runway in AvailableRunways)
{
if (runway.length >= minLength)
{
allocatedRunway = runway;
runwayRequestor.AssignRunway(runway);
}
}
if (allocatedRunway != null)
{
AvailableRunways.Remove(allocatedRunway);
}
else
{
WaitingRunwayRequestors.Add(new WaitingRunwayRequestor() { runwayRequestor = runwayRequestor, minLength = minLength });
}
}
public void ReturnRunway(Runway runway)
{
WaitingRunwayRequestor allocatedRunwayRequestor = null;
foreach (WaitingRunwayRequestor waitingRunwayRequestor in WaitingRunwayRequestors)
{
if (waitingRunwayRequestor.minLength <= runway.length)
{
allocatedRunwayRequestor = waitingRunwayRequestor;
waitingRunwayRequestor.runwayRequestor.AssignRunway(runway);
}
}
if (allocatedRunwayRequestor != null)
{
WaitingRunwayRequestors.Remove(allocatedRunwayRequestor);
}
else
{
AvailableRunways.Add(runway);
}
}
}
public class Runway
{
public readonly int length;
public readonly Terminal terminal;//same runway cannot be at 2 terminals
public Runway(Terminal terminal, int length)
{
this.terminal = terminal;
this.length = length;
}
}
public interface IRunwayRequestor
{
void AssignRunway(Runway runway);
void ReturnRunway();
}
public abstract class RunwayRequestor
{
public Runway AssignedRunway{get; set;}
public Terminal Terminal { get; set; }
public virtual void AssignRunway(Runway runway)
{
this.AssignedRunway = runway;
}
public virtual void ReturnRunway()
{
Terminal.ReturnRunway(AssignedRunway);
AssignedRunway = null;
}
}
public class Aircraft : RunwayRequestor
{
public Aircraft(Terminal terminal)
{
this.Terminal = terminal;
}
public override void AssignRunway(Runway runway)
{
base.AssignRunway(runway);
Land();
}
public void Land()
{
//perform takeoff
ReturnRunway();
}
}
public class MaintenanceCrew : RunwayRequestor
{
public MaintenanceCrew(Terminal terminal)
{
this.Terminal = terminal;
}
public override void AssignRunway(Runway runway)
{
base.AssignRunway(runway);
PerformMantenance();
}
public void PerformMantenance()
{
//perform maintenance
ReturnRunway();
}
}
class terminal
- DashDash April 23, 2013{
}
Terminal will have multiple objects of Runway
class Runway
{
string type;
Ctime t // class which holdsTime occupied
}
class Aircraft
{
}
class <NameOfAircraft> : Aircraft
{
}
In the main program I can stacks depending on the size of runways.
1 stack for small runways and second for long runways
Depending on the type of aircraft and runway runway is allocated and that stack is popped. Once it gets freed, stack gets pushed