Facebook Interview Question for SDE1s


Country: United States




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

public class LogInfo
{
	public string fun_name;
	public int first_enter_time;
	public int last_exit_time;
	public int fun_remaining_exit_count;
	
	public LogInfo(string name, string time)
	{
		fun_name = name;
		first_enter_time = time;
		fun_remaining_exit_count = 1;
	}
	
	public UpdateLogInfo(string action, int time)
	{
		if(action.Equals("enter"))
		{
			if(fun_remaining_exit_count == 0)
			{
				first_enter_time = time;
			}
			fun_remaining_exit_count++;
		}
		else if(action.Equals("exit"))
		{
			if(fun_remaining_exit_count == 0)
			{
				throw new Exception("Invalid Log Entry");
			}
			last_exit_time = time;
			fun_remaining_exit_count--;
			if(fun_remaining_exit_count == 0)
			{
				PrintLogInfo();
			}
		}
		else
		{
			throw new Exception("Invalid request");
		}
	}
	public void PrintLogInfo()
	{
		if(fun_remaining_exit_count == 0)
		{
			int inclusive_time = last_exit_time - first_enter_time;
			Console.WriteLine("{0}: inclusive time = {1}, exclusive time = {2}"
			,fun_name
			,inclusive_time > 0 ? inclusive_time : inclusive_time == 0 ? 1 : "Invalid" 
			,inclusive_time > 1 ? inclusive_time-1 : inclusive_time == 1 ? 1 :"Invalid")
		}
		else
		{
			throw new Exception("Incomplete data entry");
		}
	}
}

public void printInclusiveAndExclusiveTime (List<Log> logs)
{
	var logMap = new Dictionary<string,LogInfo>();
	foreach(var log in logs)
	{
		if(logMap.ContainsKey(log.fun_name))
		{
			logMap[fun_name].UpdateLogInfo(log.enterorExit, log.time);
		}
		else if(log.enterorExit.Equals("enter"))
		{
			logMap.Add(fun_name, new LogInfo(fun_name, log.time));
		}
		else
		{
			throw new Exception("Invalid Log Entry");
		}
	}
}

Complexity : O(N) + O(K), where N is the size of Logs, and K is the distinct entries of the log function names.

- sonesh April 24, 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