Intuit Interview Question
Senior Software Development EngineersCountry: United States
Interview Type: In-Person
Non trivial code. Absolutely non trivial. Here is one part of it, when the dates are larger than 1970 Jan 01 :
MS_IN_NORMAL_YEAR = 365 * 24* 60 * 60 * 1000
MS_IN_LEAP_YEAR = 366 * 24 * 60 * 60 * 1000
DAYS_IN_MONTH = [ 0, 31, 28 , 31, 30, 31, 30, 31, 31, 30, 31 , 30, 31 ]
def is_leap_year( year ){
return ( ( 400 /? year ) || ( !( 100 /? year ) && ( 4 /? year ) ) )
}
def get_ms_in_month( month, is_leap ){
if ( is_leap && month == 2 ) return 29 * 24 * 60 * 60 * 1000
return DAYS_IN_MONTH[month] * 24 * 60 * 60 * 1000
}
def date( millisec ){
year = 1970 // *nix inception year
is_leap = is_leap_year( year )
ms_in_year = ( is_leap ? MS_IN_LEAP_YEAR : MS_IN_NORMAL_YEAR )
while ( millisec > ms_in_year ){
millisec -= ms_in_year
year +=1
is_leap = is_leap_year( year )
ms_in_year = ( is_leap ? MS_IN_LEAP_YEAR : MS_IN_NORMAL_YEAR )
}
println ( year )
// at this point, we have found the years, now add months
month = 1
ms_in_month = get_ms_in_month ( month, is_leap )
while ( millisec > ms_in_month ){
millisec -= ms_in_month
month +=1
ms_in_month = get_ms_in_month ( month, is_leap )
}
days = (millisec / ( 24 * 60 * 60 * 1000 )) + 1
[ days, month, year ]
}
res = date ( 1476615214741 )
println( res )
- venkat2023 October 15, 2016