CSC Interview Question for Java Developers


Country: India
Interview Type: Written Test




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

I can't figure the goal of the problem. You have just described the various operations on the grid. But what is it that you are seeking to implement a solution to?

- random December 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think he's just supposed to simulate the results of these instructions.

- eugene.yarovoi December 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class ToyCar {
	
////	private ToyCar toycar;  ///// You lost it right here :(

- Arun December 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The main problem with your code is the amount of duplicate code -- you have code parts that are repeated in many different places. One of the most important quality differentiators in code is to what extent you're able to avoid this. Code duplication is bad because (among other things) when you need to change how something works, you end up needing to change it in many places. If you then forget to change one of the places, you have a bug. Another reason to avoid code duplication is because it means you will have less code, and that usually means people will be able to read it in less time.

Let's say you wanted to change the contents of one of your error messages. You would have to make the same change in many places. Also, when I read the code, it takes me more time to read everything and figure out what you've done.

To avoid code duplication, find ways to split your code up into methods. Take duplicated code and put it into its own method, and call the method everywhere that you need that code.

A somewhat more minor (but nonetheless important) issue with your code is that you should probably throw exceptions when error conditions occur. Otherwise, despite the warning messages, methods will return normally even though some invalid state has been reached. With an exception, the program would crash (if the exception is not handled), and the person using your code would immediately know they did something wrong. This is of course assuming that the desired behavior when an illegal operation has been attempted is to abort the program -- if the desired behavior is to do nothing and display a warning, you're fine.

- eugene.yarovoi December 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'm certainly no expert, so take this with a grain of salt.
Your solution is somewhat messy. You're tailor-making it for exactly this one situation, when in fact it's likely that production code would be reused or the situation in which it it was used would change.

In your code, a change to the 'board' would necessitate messing with the toycar class. What if you wanted to add two toycars to the same board in the future? What if you want to move a toycar from one board to the other? What if you want to add ToyTree objects as well?
And there's also the problem of code duplication a previous poster mentioned.

Below is my object-oriented solution, I'm not saying it's perfect or even very good, but it does make the code more flexible and extensible for future use by separating out the concept of a Board and the concept of a ToyCar for starters:

package toycar;

import java.util.HashSet;
import java.util.Set;

public class ToycarGame {

//	This was at a recent written test...
//	There is a toycar placed on a 5 by 5 board.We can give 5 commands to it , 
//	PLACE(X,Y,F) where x denotes X Axis,y denotes Y Axis and F denotes the direction to which its facing.
//	MOVE->Move will move the toycar one step in the direction where its facing
//	LEFT->Left will turn the toycar by 90 degrees to its left and face it to the new direction .Note:Left will not move the toycar, it will just change the direction
//	RIGHT->Right will turn the toycar by 90 degrees to its right and face it to the new direction .Note:Right will not move the toycar, it will just change the direction
//	REPORT->Report shall tell me the X Axis,YAxis and Direction of the toycar. like 0,0,NORTH
//
//	Note:You cannot move,left,right,report the toycar unless you place it.
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Board board = new Board(5, 5);
		ToyCar car = new ToyCar();
		try {
			car.setSurface(board, 0, 0, 0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(car.report());
		car.moveForward();
		System.out.println(car.report());
		car.turnRight();
		car.moveForward();
		System.out.println(car.report());
		car.turnRight();
		car.moveForward();
		System.out.println(car.report());
		car.turnRight();
		car.moveForward();
		System.out.println(car.report());
		car.turnRight();
		car.moveForward();
		System.out.println(car.report());		
	}

	public static class Board
	{
		Set <ToyCar> cars = new HashSet();
		int w, h;
		
		public Board(int w, int h)
		{
			this.w = w;
			this.h = h;
		}
		
		public boolean checkConstraints(int x, int y) 
		{
			if(x < 0 || x >= w || y < 0 || y >= h)
				return false;
			else
				return true;
		}
		
		
	}
	
	public static class ToyCar
	{
		int x, y;
		//0 = up, 1 = right, 2 = down, 3 = left
		int facing = 0; 
		Board surface;
		boolean placed = false;
		
		public ToyCar()
		{
		}
		
		public void setSurface(Board board, int x, int y, int facing) throws Exception
		{
			this.x = x;
			this.y = y;
			this.facing = facing;
			this.surface = board;
			if(!surface.checkConstraints(x, y))
				throw new Exception("Invalid Position");
			
			if(surface != null)
				placed = true;
			else
				placed = false;
		}

		public boolean moveForward()
		{
			switch(facing)
			{
			case 0:
				return moveTo(x, y+1);
			case 1:
				return moveTo(x+1, y);
			case 2: 
				return moveTo(x, y-1);
			case 3: 
				return moveTo(x-1, y);					
			}
			return false;
		}

		private boolean moveTo(int x, int y)
		{
			if(surface.checkConstraints(x,y))
			{
				this.x = x; 
				this.y = y;
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public void turnLeft()
		{
			if(facing == 0)
				facing = 3;
			else
				facing--;
		}
		
		public void turnRight()
		{
			facing = (facing+1)%4;
		}
		
//		private void setFacing(int facing) {
//			this.facing = facing;
//		}

		public String report()
		{
			String dir = "";
			switch(facing)
			{
			case 0:
				dir = "NORTH";
				break;
			case 1:
				dir = "WEST";
				break;
			case 2: 
				dir = "SOUTH";
				break;
			case 3: 
				dir = "EAST";
				break;
			}
			return x + ", " + y + ", " + dir;
		}
		
		public int getX() {
			return x;
		}

		public int getY() {
			return y;
		}

		public int getFacing() {
			return facing;
		}
		
	}
	
}

- Gor December 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void turnLeft(){
		turn(true);
	}

	public void turnRight(){
		turn(false);
	}	

	private void turn(boolean toLeft){
		if(currDirection.equals(EAST)){
			currDirection = toLeft ? NORTH : SOUTH;
		}else if(currDirection.equals(WEST)){
			currDirection = toLeft ? SOUTH : NORTH;
		}else if(currDirection.equals(NORTH)){
			currDirection = toLeft ? WEST : EAST;
		}else {
			currDirection = toLeft ? EAST : WEST;
		}
	}
	public boolean move(){
		if(hasScopeToMove()){
			if(currDirection.equals(EAST)){
				xAxis++;				
			}else if(currDirection.equals(WEST)){
				xAxis--;
			}else if(currDirection.equals(NORTH)){
				yAxis++;
			}else{
				yAxis--;
			}
			return true;
		}
		System.out.println("There is no scope to move ....");
		return false;
	}	
	private boolean hasScopeToMove(){
		boolean hasScope = false;
		if(currDirection.equals(EAST)){
			hasScope = maxPositiveXAxis != xAxis;				
		}else if(currDirection.equals(WEST)){
			hasScope = maxNegativeXAxis != xAxis;			
		}else if(currDirection.equals(NORTH)){
			hasScope = maxPositiveYAxis != xAxis;
		}else{
			hasScope = maxNegativeYAxis != xAxis;	
		}
		return hasScope;
	}

	public void report(){
		System.out.println(xAxis+", "+yAxis+", "+currDirection);
	}

- shijujohn1094 September 12, 2013 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More