Amazon Interview Question
SDE1sCountry: United States
Use whichever date/time method OS supplies. It will be updated to a functional version periodically.
Sample current time, extract month and year number. Extract month and year from supplied string as integers. Compare year against year, then month against month if necessary. Current year shall not be smaller than card year, if equal, current month shall not be smaller than card month.
What kind of question is this anyway? How do you even screw it up so it doesn't work in 100 years? That's the real question here.
public static boolean isValid(String date) {
java.text.SimpleDateFormat sm = new java.text.SimpleDateFormat("YYYY/MM");
String[] sms = sm.format(new java.util.Date()).split("/");
String[] dates = date.split("/");
try {
if(Integer.parseInt(dates[1] + dates[0]) < Integer.parseInt(sms[0] + sms[1])) return false;
else return true;
} catch (Exception e){
return false;
}
}
it need to work 100 years from now but the format is mmyy, if you use base 10 you can only encode 100 year but it won't be from now. it will work from xx00 to xx99 only with xx for our case is 20.
My solution will be to use a different base, for exemple base 16. we will have it working for 256 years from 00 with in our case in 2017 menas 249 years from now.
Use whichever date/time method OS supplies. It will be updated to a functional version periodically.
- Anonymous May 07, 2017Sample current time, extract month and year number. Extract month and year from supplied string as integers. Compare year against year, then month against month if necessary. Current year shall not be smaller than card year, if equal, current month shall not be smaller than card month.
What kind of question is this anyway? How do you even screw it up so it doesn't work in 100 years? That's the real question here.