Curious Clock


Benjamin recently bought a digital clock at a magic trick shop. The seller never told Ben what was so special about it, but mentioned that one day Benjamin would be faced with a surprise.

Indeed, the clock did surprise Benjamin: without warning, at someTime the clock suddenly started going in the opposite direction! Unfortunately, Benjamin has an important meeting very soon, and knows that at leavingTime he should leave the house so as to not be late. Ben spent all his money on the clock, so has to figure out what time his clock will show when it’s time to leave.

Given the someTime at which the clock started to go backwards, find out what time will be shown on the curious clock at leavingTime.

For your convenience, here is the list of months lengths (from January to December, respectively):

  • Months lengths: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31.

Please, note that in leap years February has 29 days.


For someTime = "2016-08-26 22:40" and leavingTime = "2016-08-29 10:00", the output should be
curiousClock(someTime, leavingTime) = "2016-08-24 11:20".

There are 2 days, 11 hours and 20 minutes till the meeting. Thus, the clock will show 2016-08-24 11:20 at the leavingTime.


  • [time limit] 3000ms (cs)
  • [input] string someTime

    The time at which the clock started going backwards. It is guaranteed that the time is correct and is not earlier than the midnight of January the 1st, 1970.

    The time is given in the format "YYYY-MM-DD HH:MM".

  • [input] string leavingTime

    The time at which Ben will have to leave for the meeting in the same format as someTime and with the same constraints.
    It is guaranteed that leavingTime is later than someTime, but not later than the year of 2035.

  • [output] string

    The time Ben’s curious clock will show when it’s time to leave in the same format as leavingTime and someTime. It is guaranteed that it will be not earlier than the midnight of January the 1st, 1970.


string curiousClock(string someTime, string leavingTime) {
    DateTime lt = DateTime.Parse(leavingTime);
    DateTime st = DateTime.Parse(someTime);
    TimeSpan ts = lt.Subtract(st);
    DateTime rt = st.Subtract(ts);
    return rt.ToString("yyyy-MM-dd HH:mm");



Day Of Week


int dayOfWeek(string birthdayDate) {
    DateTime date=DateTime.Parse(birthdayDate);
    bool isLeap = date.Day==29 && date.Month==2;
    DayOfWeek dow = date.DayOfWeek;
    int count=1,day=date.Day;
    for(int i=1;i<100;i++){
        date = date.AddYears(1);
        if(isLeap &&DateTime.IsLeapYear(date.Year))
            date = date.AddDays(1);
        if(dow==date.DayOfWeek && day==date.Day){
    return count;


Video Part


You have been watching a video for some time. Knowing the total video duration find out what portion of the video you have already watched.


For part = "02:20:00" and total = "07:00:00", the output should be
videoPart(part, total) = [1, 3].

You have watched 1 / 3 of the whole video.


  • [time limit] 3000ms (cs)
  • [input] string part

    A string of the following format "hh:mm:ss" representing the time you have been watching the video.

  • [input] string total

    A string of the following format "hh:mm:ss" representing the total video duration.

  • [output] array.integer

    An array of the following format [a, b] (where a / b is a reduced fraction).


int[] videoPart(String part, String total) {
    class Helper {
            int getSeconds(String time) {
                int h = Integer.parseInt(time.substring(0, 2)),
                        m = Integer.parseInt(time.substring(3, 5)),
                        s = Integer.parseInt(time.substring(6, 8));
                return h * 60 * 60 + m * 60 + s;

            int gcd(int a, int b) {
                while (a > 0) {
                    int tmp = a;
                    a = b % a;
                    b = tmp;
                return b;
    Helper h = new Helper();

    int partTime = h.getSeconds(part);
    int totalTime = h.getSeconds(total);
    int divisor = h.gcd(partTime, totalTime);
    return new int[]{partTime / divisor, totalTime / divisor};


Valid Time


Check if the given string is a correct time representation of the 24-hour clock.


  • For time = "13:58", the output should be
    validTime(time) = true;
  • For time = "25:51", the output should be
    validTime(time) = false;
  • For time = "02:76", the output should be
    validTime(time) = false.


  • [time limit] 3000ms (java)
  • [input] string time

    A string representing time in HH:MM format. It is guaranteed that the first two characters, as well as the last two characters, are digits.

  • [output] boolean

    true if the given representation is correct, false otherwise.


boolean validTime(String time) {
    class Helper {
        int charToInt(char symbol) {
        return symbol - '0';
    Helper h = new Helper();

    int hours = h.charToInt(time.charAt(0)) * 10 + h.charToInt(time.charAt(1)),
        minutes = h.charToInt(time.charAt(3)) * 10 + h.charToInt(time.charAt(4));

    if (hours < 24 && minutes < 60) {
        return true;
    return false;