Microsoft Interview Question


Country: United States
Interview Type: Phone Interview




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

I explained that, I would have the compiler put in messages onto a file, whenever a particular function is executed and when returning from that function. The format will be like below:
<TimeStamp> <Action(Start/End)> <FunctionName>

Then I'll use a stack to push function names into the stack for every action "Start" and pop out of the stack for every action "End". Keeping track of the time, along the way.
Where-in I would keep checking if the function I want to analyse is at the head of the stack.

public static int getTime(String log, String fun){
if(log == null || log.length() == 0){
return -1;
}
   Deque<String> stack = new LinkedList<>();
   String[] arr = log.split("\n");
   int currTime = 0;
   int lastTime = 0;
   for(String str: arr){
       String[] st = str.split(" ");
       int time = getTime(st[0]);
       if(st[1].equals("start"))
       {
           if(st[2].equals(fun)){
               lastTime = time;
           }
           if(!st[2].equals(fun)){
               if(stack.peek().equals(fun)){
                   currTime += time - lastTime;
               }
           }
           stack.push(st[2]);
       }
       else if(st[1].equals("end")){
           String t = stack.pop();
           if(t.equals(fun)){
                  currTime = currTime + time - lastTime;
                 // return currTime;
           }
           else{
               if(stack.peek().equals(fun)){
                   lastTime = time;
               }
           }
       }        
   }
   return currTime;
}
public static int getTime(String str){
return Integer.parseInt(str);
}
public static void main(String[] args){
StringBuilder str = new StringBuilder();
str.append("10 start fun\n");
str.append("20 start f1\n");
str.append("22 end f1\n");
str.append("30 start f2\n");
str.append("31 start f3\n");
str.append("32 end f3\n");
str.append("33 end f2\n");
str.append("70 end fun\n");

System.out.println(getTime(str.toString(), "fun"));
}

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

A code profile might need the complete call tree anyways - i would tap into the operating system/framework to dump out the full call tree into a file(starting from the main function). From there on, its a simple tree problem

double GetExecuteTime(FunCall f)
{
	double maxExecTimeSpan  = 0.0;
        foreach(var c in f.SubCalls)
        {
		if(c.ExecTimeSpan > maxExecTimeSpan )
                {  
			maxExecTimeSpan  = c.ExecTimeSpan ;
                }
        }
        return maxExecTimeSpan + f.ExecTimeSpan;
}

- Anonymous May 11, 2017 | 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