Microsoft Interview Question for Site Reliability Engineers


Country: United States
Interview Type: In-Person




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

Probably DFS?

Big O complexity is going to be the O(n * m) no matter what algorithm you apply.

First solution you provided isn't going to be any faster, even if we examine asymptotic complexity still you need to visit each of the elements.

I guess he wanted you to apply DFS to visit all elements (but to be fast we need to optimize recursion using caching or simply we can use BFS)

- Nenad December 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(n * m) no matter what algorithm you apply - that's true.
"I guess he wanted you to apply DFS" - where is sense here? he wanted DFS and faster than O(n * m) simultaneously? do you mean that interviewer did not know the time complexity of DFS?

- zr.roman December 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

why can't have start pointer and end pointer for each row. Stop the iteration when start pointer meets end pointer.

- Guru February 18, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is this possible? If there are k elements, and you need to visit all of them, how can you do it in less than O(n)? Even multi-threading does not help. Multi-threading reduces time, not time complexity because amount of total work in the end is the same.

- Noobie December 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

How multithreading reduces time?

The amount of work is still the same, we need to visit all elements, I would say that it is even slower with multithreading because of context switch

- africa1001 December 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes that's right Nenad!

- africa1001 December 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How does recursion increase the time to travel a matrix? If you are to visit each element there is no algorithm faster than O(m*n). Is there more to the question? Maybe the interviewer was waiting for you to ask more questions?

- Anonymous December 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what do you think about CPU caches? Could it be the case that amortized complexity would be less than O(m*n)?

- EPavlova December 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Review the definition of amortized complexity. No, it can't be less than O(MN).

- Anonymous December 15, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

such questions are quite strange.
Visit all elements in 2D matrix faster than O(n*m) is something like visit all elements in array faster than O(n) or visit 1 element faster than O(1).
I suppose they are senseless.

- zr.roman December 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

another my supposition is that interviewer wanted the solid answer "it is impossible, 'cause we cannot visit 1 element faster than O(1)", and all his hints were just traps, intended to confuse a candidate at a trivial question.
Why not.

- zr.roman December 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Given a swimming pool. The task is to dive deeper than the bottom of the swimming pool.
:)
Need a hint? Try to use ... <any random set of words goes here>.
All the hints are just traps, 'cause the answer is obvious.

- zr.roman December 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The only thing I can think of is using a multi-core / multiple processors and using multi-threaded solution.
For instance, since we have multiple processors available, divide the matrix into 2 equal parts and assign the 2 parts to 2 separate threads. Then we can have these two threads run simultaneously on multiple processors and get faster than O(nm) run time. Other than that I think the interviewer was trying a trick question

- puneet.sohi December 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"He said no multithreading".

- zr.roman December 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what about this? assume that all elements in the matrix are the same. just read one element. Runtime O(1).

- Anonymous December 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

then this is not a matrix, it is 1 element, the other elements are just waste of space.
Moreover, we cannot do such assumptions, because we are not given input, we are given only an algorithm. The input can be whatever. Even if all the elements in the array are the same, we will get to know this only after we have visited all the elements.

- zr.roman December 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It depends what means for them to actually "read". If you want to provide an interface let's name it "IPossibleToRead" that exposes a single read method: Read (int row, int column) and maybe two additional properties: ColumnLength, RowLength and you could return an implementation of this interface in O (1) time. Next, if he REALLY want to read he could just call our Read method.

- aurelian.lica December 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

May be, this problems about "find X in 2d array using method binary search".

- Trushnikov.Ivan December 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Cosider following recursive approach

visit (lower_bound_row,upper_bound_row,columns)
{
   if( lower_bound_row == upper_bound_row)
   {
         PRINT [lower_bound_row][0]........[lower_bound_row][columns]
   }
   else
   {
        mid = (lower_bound_row + upper_bound_row)/2;
        PRINT [mid][0]......[mid][columns];
        visit(lower_bound_row,mid-1,columns);
        visit(upper_bound_row,mid+1,columns);
   }
}

Let's say size of the array is n*m
Then following is the time complexity

T(n*m) = 2.T(nm/2) + o(m)

Applying master theorem we get

T(nm)=mlog(nm) [here b = 2, a = 2]

so , mlog(nm) is lesser than nm.
So, in this way array can be traversed with faster than nm.

Please let me know If I am doing something wrong.

- Masiur Rahaman December 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I do not think you can use the Master Theorem like that. Your recurrence relation, T(nm) = 2T(nm/2) + o(m), is not of the correct form. You need to use the same n throughout. You use nm in some spots, and just m in another.

For example, you could use it for an algorithm with the recurrence T(nm) = 2T(nm/2) + o(nm), which is O(mn log(mn)).

For your algorithm, you need to use a different approach.

- Brad January 06, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about a bidirectional BFS. Start traversing from (0,0) and (n,n), this should be half the time of a normal start to end.

- mani January 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's impossible to do less than O(mn), here is the solution by using DFS recursion.

public static void PrintDFSMatrix(int[,] myInt, int x, int y)
        {
            if (x < myInt.GetLength(0) && y < myInt.GetLength(1))
            {
                PrintDFSMatrix(myInt, x + 1, y + 1);


                for (int i = y; i < myInt.GetLength(0); i++)
                {
                    Console.WriteLine(myInt[x, i]);
                }

                for (int i = x + 1; i < myInt.GetLength(1); i++)
                {
                    Console.WriteLine(myInt[i, y]);
                }
            }
       }

- Neelavan April 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

GUYS relax it was a "stress interview" stage probably, where he had to see how candidate reacts on false and misleading information from the "boss" (who is the interviewer at that moment). He just had to explain patiently why he thinks there is no better solution that O(m*n). IMHO

- mimi August 27, 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