Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

class Turtle{
    char dir;
    int x;
    int y;
    Turtle(){
        dir= 'N';
        x=1;y=1;
    }
    void moveLeft(){
        if(dir=='N')
            dir= 'W';
       else if(dir=='W')
            dir='S';
       else if(dir=='S')
            dir='E';
        else dir='N';
        System.out.println(dir);
    }
    void moveRight(){
        if(dir=='N')
            dir= 'E';
        else if(dir=='E')
            dir='S';
        else if(dir=='S')
            dir='W';
        else dir='N';
        System.out.println(dir);
    }
    void moveForward(){
        if(dir=='N')
            y++;
        else if(dir=='S')
            y--;
        else if(dir=='E')
            x++;
        else x--;
    }
    
}

public class Solution {
    
    public static Turtle getFinalPosition(String dir,Turtle turtle){
        if(dir.length()==0){
            return turtle;
        }
          
        for(int i=0;i<dir.length();i++){
            if(dir.charAt(i)=='F')
                turtle.moveForward();
            else if(dir.charAt(i)=='L')
                turtle.moveLeft();
            else turtle.moveRight();
        }
        return turtle;
    }
    
 public static void main(String[] args) { 
     String dir ="FFFRRFLF";
    Turtle temp= new Turtle();
    getFinalPosition(dir,temp);
     System.out.println(temp.x+" "+temp.y+" "+temp.dir);
}
}

- Anonymous March 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class Tuttle {

	char dir;

	public char getDir() {
		return dir;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	int x, y;

	public Tuttle(char direction, int xPos, int yPos) {
		dir = direction;
		x = xPos;
		y = yPos;
	}

	public void moveTuttleAround(String movement) {
		for(char step : movement.toCharArray()){
			switch (step) {
			case 'F':
				moveForward();
				break;
			case 'R':
				moveRight();
				break;
			case 'L':
				moveLeft();
				break;
			default:
				System.out.println("Invalid character found");
				return;
			}
		}
	}

	public void moveForward() {
		switch (dir) {
		case 'N':
			y++;
			break;
		case 'E':
			x++;
			break;
		case 'W':
			x--;
			break;
		case 'S':
			y--;
			break;
		default:
			break;
		}
	}

	public void moveLeft() {
		switch (dir) {
		case 'N':
			dir = 'W';
			break;
		case 'E':
			dir = 'N';
			break;
		case 'W':
			dir = 'S';
			break;
		case 'S':
			dir = 'E';
			break;
		default:
			break;
		}
	}

	public void moveRight() {
		switch (dir) {
		case 'N':
			dir = 'E';
			break;
		case 'E':
			dir = 'S';
			break;
		case 'W':
			dir = 'N';
			break;
		case 'S':
			dir = 'W';
			break;
		default:
			break;
		}
	}
	public static void main(String[] args) {
		Tuttle tuttle = new Tuttle('N', 1, 1);
		
		tuttle.moveTuttleAround("FFFRRFLF");

		System.out.println("Direction : " + tuttle.getDir() + " " + tuttle.getX() + ", "+ tuttle.getY());
	}

}

- Rimmy April 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In the problem the gird is NxN, that means the grid boundary's should be set and also we need to place some obstacles in the defined gird. So that the movement of the turtle should not happen near the boundary and obstacles.

- Raj k Kumar June 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In the problem the gird is NxN, that means the grid boundary's should be set and also we need to place some obstacles in the defined gird. So that the movement of the turtle should not happen near the boundary and obstacles.

- Raj k Kumar June 11, 2018 | 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