Facebook Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

Step 1: Sort the time interval based on starting time
Step 2: Iterate through sorted interval and compare each interval with next interval and see whether it overlaps or not. if all interval overlaps entire time is covered

- Anonymous April 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Also make sure to do the last check with the last entry's end time with first entry's start time.

- bhuleskar April 25, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

This algorithm just work for this case:
1-3 2-4 3-7 6-2
However does not work for
1-7 2-4 3-5
nor
1-4 5-6 3-2

- Ian May 06, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

1-6, 2-5, 3-5 all overlap but nothing is scheduled for day 7

- Anonymous October 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

1-6, 2-5, 3-5 all overlap but nothing is scheduled for day 7

- Anonymous October 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

step 1: sort the interval based on starting time
step 2: iterate over interval and check whether all intervals overlaps with next interval in sorted array then it covers entire time of week.

- steph curry April 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Maintain a bitset of size(7*24*60*60) i.e. num of seconds in a week.
Now, for each interval, mark these bits true.

In the end, if all bits are 1, then the entire week is covered, else not.

Assuming the input to be number of seconds since Sunday 00:00:00, there will be 2
Special cases:
1. an interval spans more than a week, we need to immediately return true.
2. an interval starts at let's say friday and ends on tuesday next week.
For this, we need to set bits for friday-saturday night, and shift offsets of sunday-tuesday by 7*24*60*60

- mytestaccount2 April 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That would be too much of work and computation. Though this would work I would not prefer this.

- bhuleskar April 25, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This algorithm makes use of minimum heap to short the non-crossing interval (here after extent).

One assumption this algorithm makes is that 00:00:00 of last day of week equals to (7*24*60*60).

This algorithm divide input intervals into two groups:
1) non-crossing intervals, where start < end
2) crossing intervals where start > end

Running Time will be O(N log N) due to the heap operations (add, extract), where N is the number of input intervals.

Any suggestions or critics are welcomed.

struct extent {
  int start;
  int end;
  extent ():start(-1), end(MAX_INT32){}
}

bool is_overlap(extent* list, int n)
{
  int c_start = -1; c_end = MAX_INT32;
  MinHeap heap;
  for(int i = 0; i < n; i++)
  {
    if (list[i].start == list[i].end)
      return true;
    else if (list[i].start < list[i].end)
      heap.add(list[i]);
    else {
      if (c_start < list[i].start)
        c_start = list[i].start;
      if (c_end > list[i].end)
        c_end = list[i].end;
    }
  }

  bool no_cross = false;
  no_cross = (c_start == -1);
  extent *min;
 
  while (heap.len() > 0)
  {
    min = heap.extract();
    if (no_cross) {
      if (c_start == -1)
      {
        c_start = min->start;
        c_end = min->end;
      } else if (c_start < min->start)
        return false;
      else
        c_end = min->end;
    } else {
      if (c_start < min.start)
        return false;
      else
        c_start = min.end;
    }
  }
  return (c_start == 0 && c_end == END_OF_WEEK) || (c_start >= c_end);
}

- hankm2004 June 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can be solved using a balanced BST, such as Redblack tree (Using TreeSet in java). Running time is O(N) in number of events in the input.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TreeSet;

public class WeekSchedule {

	private static final int SECONDS_IN_WEEK = 60*60*24*7;
	private TreeSet<Event> treeSet = new TreeSet<Event>();
	
	public void addEvent(String start, String end) {
		addRange(convertToInt(start), convertToInt(end));
		
	}
	
	public boolean spansWeek() {
		int secSum = 0;
		if (treeSet == null) {return false;}
		
		for (Event pair : treeSet) {
			secSum = secSum + (pair.end - pair.start + 1);
		}
		
		return secSum == SECONDS_IN_WEEK;
	}

	private void addRange(int start, int end) {
		
		if (end<start) {
			//split into 2 events, does not matter because they are recurring anyway
			addRange(start, SECONDS_IN_WEEK);
			addRange(1, end % SECONDS_IN_WEEK);
			return;
		}
		
		Event newPair = new Event(start, end);
		
		if (treeSet.isEmpty()) {
			treeSet.add(newPair);
			return;
		}
				
		Event ceiling = treeSet.ceiling(new Event(end,end));
		if (ceiling == null) {ceiling = treeSet.last();
		}
		
		Event floor = treeSet.floor(new Event(start, start));
		if (floor == null) {floor = treeSet.first();}
		
		Event rangeLeft = null;
		Event rangeRight = null;
		
		if (newPair.compareTo(floor) == -1
				|| newPair.compareTo(ceiling) == 1) {
			treeSet.add(newPair);
		} else {
			if (floor.inRange(newPair.start)) {
				rangeLeft = floor;
				newPair = new Event(floor.start, newPair.end);
			} else {
				rangeLeft = newPair;
			}
			
			if (ceiling.inRange(newPair.end)) {
				rangeRight = ceiling;
				newPair = new Event(newPair.start, ceiling.end);
			} else {
				rangeRight = newPair;
			}
			
			treeSet.removeAll(treeSet.subSet(rangeLeft, rangeRight));
			treeSet.remove(rangeRight);
		}
		
		treeSet.add(newPair);
	}
	
	public static final class Event implements Comparable<Event> {

		private int start = 0;
		private int end = 0;
		
		
		public Event(int left, int right) {
			super();
			this.start = left;
			this.end = right;
		}
		
		public boolean inRange(int num) {
			return num >= start && num <= end;
		}

		@Override
		public int compareTo(Event o) {
			if (this.end < o.start) {
				return -1;
			} else if (o.end < this.start) {
				return 1;
			} else {
				return 0;
			}
		}

		@Override
		public String toString() {
			return "Pair [" + start + ", " + end + "]";
		}
	}
	
	private int convertToInt(String eventTime) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("E HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
        try {
			calendar.setTime(dateFormat.parse(eventTime));
		} catch (ParseException e) {
			throw new RuntimeException("wrong date");
		}        
		
		int seconds = (calendar.get(Calendar.DAY_OF_WEEK) - 1) * 60*60*24;
		seconds = seconds + calendar.get(Calendar.HOUR_OF_DAY) * 3600 
								+ calendar.get(Calendar.MINUTE) * 60
								+ calendar.get(Calendar.SECOND);
		
		return seconds;
	}
}

- Java solution using TreeSet October 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can be solved using a balanced BST, such as Redblack tree (Using TreeSet in java). Running time is O(N) in number of events in the input.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TreeSet;

public class WeekSchedule {

	private static final int SECONDS_IN_WEEK = 60*60*24*7;
	private TreeSet<Event> treeSet = new TreeSet<Event>();
	
	public void addEvent(String start, String end) {
		addRange(convertToInt(start), convertToInt(end));
		
	}
	
	public boolean spansWeek() {
		int secSum = 0;
		if (treeSet == null) {return false;}
		
		for (Event pair : treeSet) {
			secSum = secSum + (pair.end - pair.start + 1);
		}
		
		return secSum == SECONDS_IN_WEEK;
	}

	private void addRange(int start, int end) {
		
		if (end<start) {
			//split into 2 events, does not matter because they are recurring anyway
			addRange(start, SECONDS_IN_WEEK);
			addRange(1, end % SECONDS_IN_WEEK);
			return;
		}
		
		Event newPair = new Event(start, end);
		
		if (treeSet.isEmpty()) {
			treeSet.add(newPair);
			return;
		}
				
		Event ceiling = treeSet.ceiling(new Event(end,end));
		if (ceiling == null) {ceiling = treeSet.last();
		}
		
		Event floor = treeSet.floor(new Event(start, start));
		if (floor == null) {floor = treeSet.first();}
		
		Event rangeLeft = null;
		Event rangeRight = null;
		
		if (newPair.compareTo(floor) == -1
				|| newPair.compareTo(ceiling) == 1) {
			treeSet.add(newPair);
		} else {
			if (floor.inRange(newPair.start)) {
				rangeLeft = floor;
				newPair = new Event(floor.start, newPair.end);
			} else {
				rangeLeft = newPair;
			}
			
			if (ceiling.inRange(newPair.end)) {
				rangeRight = ceiling;
				newPair = new Event(newPair.start, ceiling.end);
			} else {
				rangeRight = newPair;
			}
			
			treeSet.removeAll(treeSet.subSet(rangeLeft, rangeRight));
			treeSet.remove(rangeRight);
		}
		
		treeSet.add(newPair);
	}
	
	public static final class Event implements Comparable<Event> {

		private int start = 0;
		private int end = 0;
		
		
		public Event(int left, int right) {
			super();
			this.start = left;
			this.end = right;
		}
		
		public boolean inRange(int num) {
			return num >= start && num <= end;
		}

		@Override
		public int compareTo(Event o) {
			if (this.end < o.start) {
				return -1;
			} else if (o.end < this.start) {
				return 1;
			} else {
				return 0;
			}
		}

		@Override
		public String toString() {
			return "Pair [" + start + ", " + end + "]";
		}
	}
	
	private int convertToInt(String eventTime) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("E HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
        try {
			calendar.setTime(dateFormat.parse(eventTime));
		} catch (ParseException e) {
			throw new RuntimeException("wrong date");
		}        
		
		int seconds = (calendar.get(Calendar.DAY_OF_WEEK) - 1) * 60*60*24;
		seconds = seconds + calendar.get(Calendar.HOUR_OF_DAY) * 3600 
								+ calendar.get(Calendar.MINUTE) * 60
								+ calendar.get(Calendar.SECOND);
		
		return seconds;
	}
}

- Java solution using TreeSet October 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using Balanced BST to merge ranges (TreeSet in java)

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TreeSet;

public class WeekSchedule {

	private static final int SECONDS_IN_WEEK = 60*60*24*7;
	private TreeSet<Event> treeSet = new TreeSet<Event>();
	
	public void addEvent(String start, String end) {
		addRange(convertToInt(start), convertToInt(end));
		
	}
	
	public boolean spansWeek() {
		int secSum = 0;
		if (treeSet == null) {return false;}
		
		for (Event pair : treeSet) {
			secSum = secSum + (pair.end - pair.start + 1);
		}
		
		return secSum == SECONDS_IN_WEEK;
	}

	private void addRange(int start, int end) {
		
		if (end<start) {
			//split into 2 events, does not matter because they are recurring anyway
			addRange(start, SECONDS_IN_WEEK);
			addRange(1, end % SECONDS_IN_WEEK);
			return;
		}
		
		Event newPair = new Event(start, end);
		
		if (treeSet.isEmpty()) {
			treeSet.add(newPair);
			return;
		}
				
		Event ceiling = treeSet.ceiling(new Event(end,end));
		if (ceiling == null) {ceiling = treeSet.last();
		}
		
		Event floor = treeSet.floor(new Event(start, start));
		if (floor == null) {floor = treeSet.first();}
		
		Event rangeLeft = null;
		Event rangeRight = null;
		
		if (newPair.compareTo(floor) == -1
				|| newPair.compareTo(ceiling) == 1) {
			treeSet.add(newPair);
		} else {
			if (floor.inRange(newPair.start)) {
				rangeLeft = floor;
				newPair = new Event(floor.start, newPair.end);
			} else {
				rangeLeft = newPair;
			}
			
			if (ceiling.inRange(newPair.end)) {
				rangeRight = ceiling;
				newPair = new Event(newPair.start, ceiling.end);
			} else {
				rangeRight = newPair;
			}
			
			treeSet.removeAll(treeSet.subSet(rangeLeft, rangeRight));
			treeSet.remove(rangeRight);
		}
		
		treeSet.add(newPair);
	}
	
	public static final class Event implements Comparable<Event> {

		private int start = 0;
		private int end = 0;
		
		
		public Event(int left, int right) {
			super();
			this.start = left;
			this.end = right;
		}
		
		public boolean inRange(int num) {
			return num >= start && num <= end;
		}

		@Override
		public int compareTo(Event o) {
			if (this.end < o.start) {
				return -1;
			} else if (o.end < this.start) {
				return 1;
			} else {
				return 0;
			}
		}

		@Override
		public String toString() {
			return "Pair [" + start + ", " + end + "]";
		}
	}
	
	private int convertToInt(String eventTime) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("E HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
        try {
			calendar.setTime(dateFormat.parse(eventTime));
		} catch (ParseException e) {
			throw new RuntimeException("wrong date");
		}        
		
		int seconds = (calendar.get(Calendar.DAY_OF_WEEK) - 1) * 60*60*24;
		seconds = seconds + calendar.get(Calendar.HOUR_OF_DAY) * 3600 
								+ calendar.get(Calendar.MINUTE) * 60
								+ calendar.get(Calendar.SECOND);
		
		return seconds;
	}
}

- blckembr October 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Test
	public void test() {
		weekSched.addEvent("Tue 09:00:00", "Tue 22:00:00");
		weekSched.addEvent("Tue 09:00:00", "Thu 22:00:00");
		weekSched.addEvent("Sun 00:00:00", "Tue 08:59:59");
		weekSched.addEvent("Wed 15:00:00", "Fri 22:00:00");
		weekSched.addEvent("Wed 15:00:00", "Fri 22:00:00");
		weekSched.addEvent("Fri 22:00:00", "Sat 23:59:59");
		
		boolean actual = weekSched.spansWeek();
		logger.info("Spans week? {}", actual?"yes":"no");
		assertTrue(actual);
	}

- blckembr October 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Complexity is O(NLogN) where N is the number of events added, due to operations of the balanced tree.

- blckembr October 20, 2015 | Flag


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