The MARS ROVER problem

The problem is stated below:

A squad of robotic rovers are to be landed by NASA on a plateau on Mars.
This plateau, which is curiously rectangular, must be navigated by the
rovers so that their on-board cameras can get a complete view of the
surrounding terrain to send back to Earth.

A rover’s position and location is represented by a combination of x and y
co-ordinates and a letter representing one of the four cardinal compass
points. The plateau is divided up into a grid to simplify navigation. An
example position might be 0, 0, N, which means the rover is in the bottom
left corner and facing North.

In order to control a rover, NASA sends a simple string of letters. The
possible letters are ‘L’, ‘R’ and ‘M’. ‘L’ and ‘R’ makes the rover spin 90
degrees left or right respectively, without moving from its current spot.
‘M’ means move forward one grid point, and maintain the same heading.

Assume that the square directly North from (x, y) is (x, y+1).

The first line of input is the upper-right coordinates of the plateau, the
lower-left coordinates are assumed to be 0,0.

The rest of the input is information pertaining to the rovers that have
been deployed. Each rover has two lines of input. The first line gives the
rover’s position, and the second line is a series of instructions telling
the rover how to explore the plateau.

The position is made up of two integers and a letter separated by spaces,
corresponding to the x and y co-ordinates and the rover’s orientation.

Each rover will be finished sequentially, which means that the second rover
won’t start to move until the first one has finished moving.

The output for each rover should be its final co-ordinates and heading.


Test Input:
5 5
1 2 N
3 3 E

Expected Output:
1 3 N
5 1 E

The solution link:

The gist description: The source code is available in src folder.
JUnit test case is available inside test folder.
The JUnit testcases contain only test inputs given in the mail.
For inputs other than given input you can use the com.mars.rovers.Main class which takes the user inputs.


12 Comments Add yours

  1. Rajnish says:

    Hi Abhinaba. I too appeared for the thoughtwork round and I wrote the program on same problem, but unfortunately it got rejected. Reason might be I used if-elses in place of switch and no separate exception class for the program and direct use of Exception class with message.

    But as far as extendibility is concerned my program takes care of it, I mean it can support N-Dimension also. I have done the good use of interfaces in it , using the concept of design by interface. I used static 2 dimensional array for logic for setting the direction :
    private static char directions[][] = {{WEST,EAST},{EAST,WEST},{NORTH,SOUTH}, {SOUTH,NORTH}};

    I had given just 1 day for preparing it, because of regular office work.

  2. User says:

    @ Abhinaba Basu —
    And the solution given by you here is approved by TW ? I mean, was it accepted ?

  3. vino says:

    Hi Rajnish,

    May be you dont have enough abstractions and not encapsulated the objects. It doesnt look like they are silly enough to reject because you have used if instead of switch. Moreover even using switch case would make your abstractions to have more than one axis of change. Say for example your case if i say that rover can fly(I mean 3 dimensions). How many places would i need to change it in your program? I may even need to rewrite. Think of getting rid of those nasty ‘ifs’.

  4. tariMipmeri says:

    Hello.This article was really fascinating, particularly since I was looking for thoughts on this topic last Friday.

  5. om-gupta says:

    Hi .. I am looking the code..
    Could you please sent me like so that i can download the code.

    Om Gupta

  6. Sankaet says:

    Check this out:

  7. Anonymous says:

    But wanna tell that this is extremely helpful, Thanks for taking your time to write this.

  8. dmmaseoseoseoseo says:

    I was exploring the internet for some info since yesterday night and I at last found what i was looking for! This is a wonderful site by the way, but it seems to be a slight difficult to see from my i phone.

  9. Francoise Piscitello says:

    I am not a follower of India, but they were great in this world cup. Will be interesting to see if they can win away from india next time around.

  10. says:

    Hey There. I found your blog the usage of msn. This is a very well written article. I will be sure to bookmark it and return to read more of your useful information. Thank you for the post. I’ll certainly comeback.

  11. A solution in 3 lines of code –

  12. Jose says:

    I am applying for a developer position at thoughtworks and will need to write the code too. what language can I write it in? could you give me some pointers what they look for in the code? I wrote all the code using basic OOP principles and am currently refining it to make sure it ticks their boxes. Please get back to me ASAP

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s