## Facebook Interview Question

SDE1s**Country:**United States

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
```

```
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
```

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;
}
```

In Scala:

- guilhebl May 03, 2017