TrainComposition


Algorithmic thinking Data structures Public

Hard  

20min

A TrainComposition is built by attaching and detaching wagons from the left and the right sides.

For example, if we start by attaching wagon 7 from the left followed by attaching wagon 13, again from the left, we get a composition of two wagons (13 and 7 from left to right). Now the first wagon that can be detached from the right is 7 and the first that can be detached from the left is 13.

Implement a TrainComposition that models this problem.

 
 


  •   Example case: java.lang.UnsupportedOperationException at TrainComposition.attachWagonFromLeft(TrainComposition.java:3)
  •   Several wagons: java.lang.UnsupportedOperationException at TrainComposition.attachWagonFromRight(TrainComposition.java:7)
  •   Performance test with a large number of wagons: java.lang.UnsupportedOperationException at TrainComposition.attachWagonFromRight(TrainComposition.java:7)