Microsoft Interview Question
Country: United States
Interview Type: Phone Interview
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;
}
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.
- Anon April 11, 2017