Amazon Interview Question for SDE-2s


Team: Robotics
Country: United States
Interview Type: In-Person




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

struct Job
{
	int start;
	int end;
};

struct JobPoint
{
	bool operator<(const JobPoint& p)
	{
		return time != p.time ? time < p.time : (IsStart && (!p.IsStart));
	}

	int time;
	bool IsStart;
};

int MinimalVMs(const vector<Job>& jobs)
{
	vector<JobPoint> jps;
	for (const Job& job : jobs)
	{
		jps.emplace_back(JobPoint{ job.start, true });
		jps.emplace_back(JobPoint{ job.end, false });
	}

	sort(jps.begin(), jps.end());

	int max_num = 0, cur_num = 0;
	for (int i = 0; i < jps.size(); i++)
	{
		if (jps[i].IsStart)
		{
			cur_num++;
			max_num = max(max_num, cur_num);
		}
		else
			cur_num--;
	}

	return max_num;
}

- gemini May 24, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

put starttime and endtime in one array and sort it ( be able to differentiate which is start and which is end). Scan the array for concurrently running program and keep track of max value for the same. after complete of scan return the max value.

- Mukul June 14, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Add 2 array, one for start & other for end. Sort both the arrays.
starting from end index=0, compare the start time with end idx time.

int[] start=new int[array.Length];
int[] end=new int[array.Length];
for(int i=0;i< array.Length;i++)
{
	start[i]=array[i][0];
	end[i]=array[i][1];
}
Array.Sort(start);
Array.Sort(end);
int numofVM=0;
for (int i=0;i<array.Length;i++ )
{
If (start[i]<=end[endidx] )
	numofVM++;
 else 
 endidx+1; 
}
return numofVM;

- nerdyelf July 02, 2019 | 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