Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Written Test




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

package com.jetti.crates;

public class Crate {

	int xCord;
	int yCord;

	public Crate(int xCord, int yCord) {
		super();
		this.xCord = xCord;
		this.yCord = yCord;
	}

	public int getxCord() {
		return xCord;
	}

	public int getyCord() {
		return yCord;
	}

	@Override
	public String toString() {
		return "\nCrate  : [x-Cord: " + xCord + ", y-Cord: " + yCord + "]";
	}

}

package com.jetti.crates;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static java.lang.Math.sqrt;

public class MoveCrates {
	
	List<Crate> movedCrates = new ArrayList<Crate>();

	public List<Crate> moveToNew(int N,List<Crate> cratesToMove, int M)
	{
		
		if(M<=N)
		{
			Collections.sort(
	                cratesToMove,
	                (crate1, crate2) -> 
	                (int)(sqrt((crate1.getxCord()*crate1.getxCord())+(crate1.getyCord()*crate1.getyCord())))
	                        - (int)(sqrt((crate2.getxCord()*crate2.getxCord())+(crate2.getyCord()*crate2.getyCord()))));
			
			for(int i=0;i<M;i++)
			{
				movedCrates.add(cratesToMove.get(i));
			}
			
			return movedCrates;
		}
		else
		{
			System.out.println("Oops, old WareHouse do not have "+M+"crates");
			return null;
		}
	}
}

package com.jetti.crates;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Shifting {
	
	//Number of crates in old warehouse
	int N;
	//List of all that crates in old ware house
	List<Crate> crateLocation = new ArrayList<Crate>();
	
	public static void main(String args[])
	{
		Shifting obj = new Shifting();
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the total number of crates in the old warehouse:");
		obj.N = in.nextInt();
		System.out.println("Enter the loction of each crate in x nad y cords:");
		int x=0;
		int y=0;
		
		for(int i=0;i<obj.N;i++)
		{
			System.out.println("Enter the x-cord of crate "+(i+1));
			x = in.nextInt();
			System.out.println("Enter the x-cord of crate "+(i+1));
			y = in.nextInt();
			
			Crate c = new Crate(x,y);
			obj.crateLocation.add(c);			
		}
		
		System.out.println("Enter the number of crates to be moved:");
		int M = in.nextInt();
		
		MoveCrates mobj = new MoveCrates();
if(mobj.moveToNew(obj.N,obj.crateLocation,M)!=null)
		{
		System.out.println("The following crates will be moved:");		
		System.out.println(mobj.movedCrates);
		}	
	}	
}

- Dasaradhi Jetti August 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

c#

using System;
using System.Linq;

namespace CratesToShip
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            int totalCrates = 3;
            int[][] allLocations = {new []{1, 2}, new []{3, 4}, new []{1, -1}};
            int truckCapacity = 2;
            int[][] results = CratesToShip(totalCrates, allLocations, truckCapacity);
        }

        public static int[][] CratesToShip(int totalCrates, int[][] allLocations, int truckCapacity)
        {
            return allLocations.OrderBy(location => Math.Sqrt(Math.Pow(location[0], 2) + Math.Pow(location[1], 2))).Take(truckCapacity).ToArray();         
        }
    }
}

- MS August 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

c#

using System;
using System.Linq;

namespace CratesToShip
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            int totalCrates = 3;
            int[][] allLocations = {new []{1, 2}, new []{3, 4}, new []{1, -1}};
            int truckCapacity = 2;
            int[][] results = CratesToShip(totalCrates, allLocations, truckCapacity);
        }

        public static int[][] CratesToShip(int totalCrates, int[][] allLocations, int truckCapacity)
        {
            return allLocations.OrderBy(location => Math.Sqrt(Math.Pow(location[0], 2) + Math.Pow(location[1], 2))).Take(truckCapacity).ToArray();         
        }
    }
}

- MS August 14, 2017 | 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