## Microsoft Interview Question

SDE-2s**Country:**United States

**Interview Type:**In-Person

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.

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.

- Ikj July 11, 2016A 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.