Days Since Last Login ๐
Updated
Problem statement
At Alarm.com we need a new piece of functionality that will tell us how many days it's been since a user last logged into our site. Our product manager has given us the following requirements that they would like implemented so that we can start tracking inactive users.
You are given two strings: lastLoginDate, the day the user last logged in, and today, today's date. Both use the format "MM/dd/yyyy". today is passed in so the answer does not depend on when the code runs.
Minimally Viable Product
- Convert
lastLoginDateto a date and subtract it fromtoday. - Return the absolute whole number of days as a string, with no leading zeros (for example
"0","1"or"365").
Things to consider (check them in this order)
- If
lastLoginDateis empty or contains only spaces, return the error code"E8430". - Otherwise, if
lastLoginDateis in the wrong format, return the error code"E9021". The correct format is exactly 10 characters: two digits,/, two digits,/, four digits. Anything else is the wrong format, including one-digit months or days and spaces before or after the date. - Otherwise, if
lastLoginDateis not a valid date, return the error code"E1756". A valid date has a month from01to12, a day that exists in that month, and a year from0001to9999.
Use the Gregorian leap-year rule for every year: February has 29 days when the year is divisible by 4 but not by 100, or divisible by 400.
Complete the function daysSinceLastLogin in the editor.
daysSinceLastLogin has the following parameters:
String lastLoginDate: the last login date to checkString today: today's date, always a valid date in the format"MM/dd/yyyy"
The function returns a String: the number of days between the two dates, or one of the error codes above.
Function
daysSinceLastLogin(lastLoginDate: String, today: String) โ StringExamples
Example 1
lastLoginDate = "07/06/2022"today = "07/07/2022"return = "1"Both strings are valid dates. 07/06/2022 is one day before 07/07/2022, so the answer is "1".
Example 2
lastLoginDate = "02/30/2020"today = "07/07/2022"return = "E1756"The input "02/30/2020" is in the correct format but is not a valid date, because February does not have 30 days. So the function returns the error code "E1756".
Constraints
0 โค lastLoginDate.length โค 20lastLoginDatecontains only printable ASCII characters (codes 32 to 126), so the only whitespace is the space character.today.length = 10todayis a valid date in the format"MM/dd/yyyy"with a year from0001to9999.
More Alarm.com interview questions
Similar questions at other companies
- Branch with the Most FilesCapital One ยท OACapital One interview questions
- Robot Stops Left or Right (Forward Deployed Engineer)Boston Consulting Group ยท OABoston Consulting Group interview questions
- Blood Type Inheritance: Possible Parent GenotypesAbridge ยท OAAbridge interview questions
- REST API: Highest International StudentsRippling ยท OARippling interview questions
- Rearrange String by Alternating EndsHudson River Trading ยท OAHudson River Trading interview questions
- Filter Isolated Interior Signal BitsSEDEMAC ยท OA
- Scale Ingredient QuantitiesUpstart ยท OAUpstart interview questions
- Telephone Keypad String EncodingEpic ยท OAEpic interview questions
- Count Invalid Log GroupsIBM ยท OAIBM interview questions
- Count Case-Insensitive TripletsTikTok ยท OATikTok interview questions