Facebook Interview Question for SDE1s


Country: United States




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

In Scala:

object Main {
  def main(args: Array[String]): Unit = {
    println(getClosestYear(99))
    println(getClosestYear(15))       
    println(getClosestYear(0))    
    println(getClosestYear(18))   
    println(getClosestYear(10))   
    println(getClosestYear(6))   
    println(getClosestYear(2))   
    println(getClosestYear(1))   
  }
  
  def getClosestYear(input:Int): Int = {
    val currentYear = Calendar.getInstance().get(Calendar.YEAR)
    val last2Digits = currentYear % 100    
    if (input > last2Digits) currentYear - 100 - last2Digits + input 
    else currentYear - last2Digits + input    
  }
}

- guilhebl May 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

closestYear(18) - answer coming to be 1918...shud be 2018 correct?

import datetime
def closestYear(input):
  now = datetime.datetime.now()  
  last2Digits=now.year%100
  c1=now.year - 100 - last2Digits + input
  c2=now.year - last2Digits + input
  #print "input,c1,c2",input,c1,c2
  if abs(now.year-c1)>abs(now.year-c2):
      print "input",str(input),":",c2
  else:
      print "input",str(input),":",c1

- @TryHard May 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import datetime
def closestYear(input):
  now = datetime.datetime.now()  
  last2Digits=now.year%100
  c1=now.year - 100 - last2Digits + input
  c2=now.year - last2Digits + input
  #print "input,c1,c2",input,c1,c2
  if abs(now.year-c1)>abs(now.year-c2):
      print "input",str(input),":",c2
  else:
      print "input",str(input),":",c1

- @TryHard May 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another horribly-worded question.

int windowYear (int y) {
    return y < (50 + (DateTime.Now.Year % 100)) ? 2000 + y : 1900 + y;
}

This simplest solution makes a few assumptions:
1. 0 <= y < = 99
2. the current year should always be the center-point on which the window should pivot
3. the centuries in play will always be 1900 and 2000
4. no actual years from the range 0 to 99 will ever be valid
5. there are no better ways to deal with two digit years.

Let's examine these assumptions:
1. To imply that the input values are constrained elsewhere implies that they could just as easily be constrained to four digit years. Of course, this just postpones the problem for the next 7983 years when programmers will have to deal with five-digit years. If these years are coming from user input, it would be better to enforce a four digit year with an input mask. If the data is coming from a database where years were only stored in two digits (did we learn nothing from Y2K?), then we probably need to look at what this year represents in order to pick an appropriate pivot (see point 2).

2. If the two-digit year represents something like a person's birthdate, a much better pivot would be (1 + (currentyear % 100)) so that your 99 year old grandmother's birthday of 6/1/18 will correctly pivot to 6/1/1918 instead of 6/1/2018. The situation is reversed if the date represents something like "expected retirement date."

3. I would hope (but not expect) that a hack like this would be caught in a code review and rejected as the brittle hack that it is. Let's hope code like this won't be needed in the next century.

4. What if a two-digit year is valid and should not be converted? Say you're answering an online quiz which asks, "In what year did the Roman emperor Augustus die?" You type in the correct answer, 14, and the website converts this to 2014 and your answer is scored as incorrect.

5. The only reason to do a year windowing function like this is if you have something like a database that already contains two-digit years. If you're collecting this data on an ongoing basis, fix the problem at the point of data entry.

So, at the very least, modify this function to include the pivot. The pivot basically defines how many years into the future will be windowed into the 2000s

int windowYear (int y, int pivot) {
    return y < (pivot + (DateTime.Now.Year % 100)) ? 2000 + y : 1900 + y;
}

- cjudge@grandecom.net May 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Given the way this question was worded, it is unclear which century one should ascribe the year to.

- Anonymous May 05, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The easiest way to do this is to find the tipping point:

python
def get_closest(y):
    cutoff = 67  # split the difference, i.e. (2017 - 50) % 100
    offset = 1900 if y > cutoff else 2000
    return offset + y

- Kristian Holsheimer May 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int ClosestYear(int current_year, int two_digits)
{
	int year1 = (current_year - current_year % 100) + two_digits;
	int year2 = year1 > current_year ? year1 - 100 : year1 + 100;
	return abs(year1 - current_year) < abs(year2 - current_year) ? year1 : year2;
}

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

public class Closestyear {
	
	int closestyear(int y,int median){
		if((median+100-y)<(median+y)){
			return(median-100+y);
		}else{
			return(median+y);
		}
	}
	
	public static void main(String[] args) {
		Closestyear find = new Closestyear();
		System.out.println(find.closestyear(99, 2000));
	}
}

- Sathiyajith Babu July 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

and

public class Closestyear {

int closestyear(int y,int median){
if((median+100-y)<(median+y)){
return(median-100+y);
}else{
return(median+y);
}
}

public static void main(String[] args) {
Closestyear find = new Closestyear();
System.out.println(find.closestyear(99, 2000));
}
}

and

- Sathiyajith Babu July 28, 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