Microsoft Interview Question for SDE-2s


Country: United States
Interview Type: In-Person




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

One way is to copy the spirally sorted matrix into an array and perform the insertion and deletion there and then copy the modified array spirally onto the matrix.

A better way is if the value to be inserted is less than the element to be deleted, you can traverse the matrix spirally and insert the element at that position and keep "pushing" the values present forward till you reach the element to be deleted.
If the value to be inserted is greater than the value to be deleted, then keep pulling the elements backwards. If the value to be inserted is the same as the value to be deleted, leave the matrix unchanged.

- Ikj July 11, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can be done pretty easily in O(n) time using spiral traversal of matrix. Any O(log n ) ideas?

- Sibendu Dey June 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What's the O(n) solution ? The solution i could think of traversing the matrix and maintaining distance..when the dist is minimum, replace that element with the given element..finally check the spiral sorted order

- Anonymous June 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be solved by using appropriate data structure linked list may be,
give a matrix of matrixN*matrixN

template <typename T, typename... Args>
T minAll(T a, T b, Args... args)
{
	return minAll(a < b ? a : b, args...);
}

template <typename T> T minAll(T t, T x) {
	return (t < x ? t : x);
}

Index of (i,j) to index of Linked list is

int MatrixRemove(int i, int j)
{
	int depth = minAll(i, j, matrixN - i, matrixN - j);
	cout << "Depth " << depth << endl;
	int start=0;
	if (depth != 0)
	{
		for (int i=1;i<=depth;i++)
			start = start + (matrixN-2*(i-1))*4;
	}
	cout << "Start " << start << endl;

	if (j >= i)
		return start + (i - depth) + (j - depth);
	else
		return start+((matrixN - 2*depth)*4 - (i-depth + j-depth));
}

find the value take O(logn) in sorted list and insertion will also take O(logn) in sorted list.

- raghu.aok June 16, 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