Microsoft Interview Question for Software Engineer / Developers


Team: Bing
Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
4
of 8 vote

use a stack to store the function not returned, if read a log entry that a function starts, push the entry. if read a log entry that a function ends, pop up an entry from the stack

when read an entry and the current running function is "foo" by checking whether the top element of the stack, add the time difference between the current entry and the previous one to the total

Time: O(n)

Pseudo Codes

enum Type 
{
     START, 
     END
};

class LogEntry
{
     public DateTime timeStamp;
     public String function;
     public Type type;
}

public int getElapse(List<LogEntry> log, String function)
{
     int elapse = 0;
     Stack<LogEntry> s = new Stack<LogEntry>();
     for(int i = 0; i < log.size(); i++) {
          LogEntry entry = log.get(i);
          if(!s.empty() && s.peek().function.equals(function)) {
               elapse += entry.timeStamp - log.get(i - 1).timeStamp;
          }
          if(entry.type == Type.END && s.peek().type == Type.Start && entry.function.equals(s.peek().function)) {
               s.pop();
          } else {
               s.push(entry);
          }
          return elapse;
}

- dgbfs November 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

lfenjoy9, Great job! Looks like it is a perfect O(n) solution!

- Igor November 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this is not that easy and needs some modifications. We will be using a stack again.
- When there is a foo.start we will log the time at the stack.
- When we see another function start we check the time at the top of the stack and if there is a start of foo time we take the difference and add it to the total time. We also update the information for the foo start time as for later use.
- When we see a foo end we pop the last foo start and add the time difference to the total time.
- If there is an end for another function we will check the foo stack and update the start time of foo start to the end time of the other function.
I wish I had written the code but I couldn`t right now. Please share your comments. Thanks...

- bluewish December 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Bluewish, could you please write what's wrong with lfenjoy9's code? Please, give an example when it will not work.

- Igor December 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

"(10:20-10:12)" this one is not calculated, like:
foo2()_end xxx
foo()_end xxx

- Anonymous December 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ok, I agree

- Igor December 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think (10:20-10:12) was already calculated. because of
elapse += entry.timeStamp - log.get(i - 1).timeStamp;
That means that even if (foo2()_end, 10:12) was pop up before (foo()_end, 10:20) was push in, log.get(i - 1) was not affected.

- mmxmw January 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

mmxmw, I think, foo2()_end will be poped up without any changes inside elapse, because the condition: "s.peek().function.equals(function)" will return false. So, we'll lose any information about "10:12" time

- Igor January 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the logic seems little convoluted (for me). What we are trying to achieve is Total time taken in funA is = (endA - endB) - sum of time spend in any other function.
We use a stack to track the function. Whenever you pop an element from the stack, you check if its the given function. If yes, then add difference. If its some other function, subtract the difference.

- SR January 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do you have an approach?

- Anonymous November 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

my solution will use a stack, as long as start is found push into stack.when an end is encountered, pop the first start line of the similar function

- Anonymous November 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what if under multi-thread ormulti process situation? like:
(foo()_start, 10:01);
(foo2()_start, 10:03);
(foo()_start, 10:05);
(foo2()_end, 10:08);
(foo()_end, 10:12);
(foo()_end, 10:20);

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

It's impossible (interviewer told me that log is correct).
(foo2()_start, 10:03);
(foo()_start, 10:05);
(foo2()_end, 10:08); // <- first foo should end, because log is correct

- goRGon December 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

BTW, ifenjoy9 already gave a wonderful solution here:
careercup.com/question?id=14990939

- goRGon December 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Given how the problem is stated, you have a log file with the format above. If you set it up to have a LogEntry object like described above, then you will have to iterate through the list twice, which is not bad but there is a more efficient way to do it.
- Scan through the file, look for where the function first occur.
- Set startFunct = 1, endFunct = 0; Setting it this way, startFunct will always lead endFunct unless the corresponding ending function matches with the originally calling function.
- Create a variable to keep track of the time for even iteration count evenTime, and another one for odd iteration count oddTime. Time elapse is the difference between the even and the odd.
- Iterate until startFunct == endFunct, increment them along the way. Add time into the evenTime and oddTime.
Here is the code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ElapseTime 
{
	
	public int getElapsedTime( String fileName, String startFunct, String endFunct ) throws IOException
	{
		int elapseTime = -1;
		if( fileName == null || startFunct == null || endFunct == null )
			return elapseTime;
		
		BufferedReader br = null;
		String currentLine = "";
		try
		{
			br = new BufferedReader( new FileReader( fileName ) );
			currentLine = br.readLine();
			while( currentLine != null && !getFunctName(currentLine).equals(startFunct) )
			{
				currentLine = br.readLine();
			}
		}
		catch( IOException e )
		{
			if( br != null )
			{
				br.close();
			}
			return -1;
		}
		
		int startCount = 1;
		int endCount = 0;
		int oddTime = getTime(currentLine);
		int evenTime = 0;
		int count = 1;
		
		while( (currentLine = br.readLine()) != null && startCount != endCount )
		{
			if( getFunctName(currentLine).equals(startFunct) )
				startCount++;
			if( getFunctName(currentLine).equals(endFunct) )
				endCount++;
			
			count++;
			
			if( count % 2 == 0 )
				evenTime += getTime( currentLine );
			else
				oddTime += getTime( currentLine );
		}
		
		br.close();
		
		if( startCount == endCount )
		{
			return evenTime - oddTime;
		}
		
		return -1;
			
	}
	
	public String getFunctName( String readLine )
	{
		if( readLine == null )
			return null;
		int index = readLine.indexOf(",");
		if( index != -1 )
		{
			return readLine.substring(1, index);
		}
		
		return null;
	}
	
	public int getTime( String readLine )
	{
		if( readLine == null )
		{
			return -1;
		}
		
		int start = readLine.indexOf(",") + 1;
		int end = readLine.indexOf(":", start);
		
		if( start > 1 && end > start )
		{
			return Integer.parseInt(readLine.substring(start, end))*60 + 
					Integer.parseInt(readLine.substring(end + 1, readLine.length()-2)); 
		}
		
		return -1;
	}
	
}

- icoderttc December 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.LinkedList;
import java.util.Stack;

public class Logs {
	LinkedList<LogEntry> entries;

	public class LogEntry {
		String funName;
		int timestamp;
		String type;

		public LogEntry(String funName, int timestamp, String type) {
			super();
			this.funName = funName;
			this.timestamp = timestamp;
			this.type = type;
		}

		@Override
		public String toString() {
			return funName + " " + timestamp + " " + type;
		}

	}

	public Logs() {
		super();
		entries = new LinkedList<LogEntry>();
		LogEntry e1 = new LogEntry("foo", 1, "start");
		LogEntry e2 = new LogEntry("foo2", 3, "start");
		LogEntry e3 = new LogEntry("foo", 5, "start");
		LogEntry e4 = new LogEntry("foo", 8, "end");
		LogEntry e5 = new LogEntry("foo2", 12, "end");
		LogEntry e6 = new LogEntry("foo", 20, "end");
		entries.add(e1);
		entries.add(e2);
		entries.add(e3);
		entries.add(e4);
		entries.add(e5);
		entries.add(e6);
	}

	public int calElapse(String functionName) {
		int elapse = 0;
		Stack<LogEntry> stack = new Stack<LogEntry>();
		for (int i = 0; i < entries.size(); i++) {
			LogEntry entry = entries.get(i);
			if (!stack.isEmpty() && stack.peek().funName.equals(functionName)) {
				elapse += (entry.timestamp - entries.get(i - 1).timestamp);
			}
			if (entry.type.equals("end") && stack.peek().type.equals("start")
					&& entry.funName.equals(stack.peek().funName)) {
				stack.pop();
			} else {
				stack.push(entry);
			}
		}
		return elapse;
	}

	public static void main(String[] args) {
		Logs logs = new Logs();
		System.out.println(logs.calElapse("foo"));
	}
}

- Kevin March 02, 2013 | 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