Google Interview Question for Software Engineers


Country: United States




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

//Worst case Time Complexity: O(nlogn) where n is the number of log entries in the stream.
public class Event{
	long time;
	int tag;
}

public class Node{
	int time;
	int tag;
	Node prev;
	Node next;
	
	public Node(int tagVal,int timeVal){
		time = timeVal;
		tag = tagVal;
	}
}

public class StreamSvc{
	private TreeMap<Integer,Node> bst;
	private Set<Integer> tags;
	private int interval;
	
	
	public StreamSvc(int intervalSize){
		bst = new TreeMap<Integer,Node>();
		tagToTime = new HashSet<Integer>();
		interval = intervalSize;
	}
	
	public void readStream(Iterator<Event> it){
		while(it.hasNext()){
			Event e = it.next();
			Integer val = bst.lowerKey(e.time - interval);
			while(val != null){
				Node n = bst.get(val);
				while(n != null){
					if(n.next != null){
						n.next.prev = null;
						n.next = null;
					}
					tags.remove(n.tag);
					n = n.prev;
				}
				Integer next = bst.lowerKey(val);
				bst.remove(val);
				val = next;
			}
			if(!tags.contains(e.tag)){
				Node x = new Node(e.time,e.tag);
				if(bst.containsKey(x.time)){
					Node v= bst.get(x.time);
					v.next = x;
					x.prev = v;
				}
				bst.put(x.time,x);
				tags.add(x.tag);
			}
		}
		
		
	}

}

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

Can you explain your thought process a little bit? @divm01986

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

what's example input/output?

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

@divm01986 Is it necessary to have the tags set? You already have the tags in the tree.

Also, e.time - interval will not give you the timestamp of the last X seconds if the event is from yesterday. But probably in this kind of problem it is a normal assumption to make that the time of last event processed is the current time.Is it necessary to have the tags set? You already have the tags in the tree.

Also, e.time - interval will not give you the timestamp of the last X seconds if the event is from yesterday. But probably in this kind of problem it is a normal assumption to make that the time of last event processed is the current time.

- damian.j9687 April 17, 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