Amazon Interview Question for SDE1s


Country: United States
Interview Type: Phone Interview




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

public class Room
{
	public int Capacity { get; set; }
}

public class RoomBooking
{
	public int StartTime { get; set; }
	public int EndTime { get; set; }
	public int NumberOfPeople { get; set; }
}

 /// <summary>
 /// Greedy assignment
/// </summary>
/// <param name="booking"></param>
/// <returns></returns>
public List<Room> GetRooms(List<RoomBooking> booking)
{
	// taken - first argument is the endTime
	SortedList<int, Room> takenRooms = new SortedList<int, Room>();
	// available - first argument is room capacity
	SortedList<int, Room> available = new SortedList<int, Room>();
	
	// sort the timeIntervals
	booking = booking.OrderBy(x => x.StartTime).ToList();

	foreach (var item in booking)
	{
		// move rooms from taken to available
		int idx = takenRooms.Keys.ToList().BinarySearch(item.StartTime);
		if (idx < 0)
			idx = (~idx)-1;

		if (idx <= takenRooms.Keys.Count - 1)
		{
			for (int k = idx; k >= 0; k--)
			{
				available.Add(takenRooms.ElementAt(k).Value.Capacity, takenRooms.ElementAt(k).Value);
				takenRooms.RemoveAt(k);
			}
		}

		// if none available - create new
		if (available.Count == 0)
		{
			takenRooms.Add(item.EndTime, new Room() { Capacity = item.NumberOfPeople });
		}
		else
		{
			// find first room which satisfies condition and move it from available to taken
			available.Keys.ToList().BinarySearch(item.NumberOfPeople);
			if (idx < 0)
				idx = ~idx;

			if (idx < takenRooms.Keys.Count - 1)
			{
				var room = available.ElementAt(idx);
				available.RemoveAt(idx);
				takenRooms.Add(item.EndTime, room.Value);
			}
			else
			{
				// take the one with the biggest capacity and increase it
				var room = available.Last();
				available.RemoveAt(available.Count - 1);
				room.Value.Capacity = item.NumberOfPeople;
				takenRooms.Add(item.EndTime, room.Value);
			}
		}
	}

	return takenRooms.Select(x=>x.Value).Union(available.Select(x=>x.Value)).ToList();
}

- lx3 August 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hey! I have few doubts on how you wrote this algorithm . Because I want to know on how you decided few things:-

1) sorting the time intervals based on startTime
2) Why are you doing binarySearch for startTime
3) what are takenRooms and available that you have mentioned in your code

Iam a guy who is crazy about programming but Iam confused on how you came up this algorithm . Could you explain please ? Anyway sorry for disturbing you

- srupesh51 November 13, 2016 | 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