Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Disheartened

Options
  • 16-01-2019 11:19pm
    #1
    Closed Accounts Posts: 1,758 ✭✭✭


    So I applied for a graduate position at a large tech company and they asked me to do an online test. Now, I'm not a master coder or anything, and with it being a graduate role, I thought the idea would be to confirm I am not completely useless, which I don't believe I am.

    Well, I had 3 tasks to complete in 2.5 hours, and I didn't even get past the first one!! I was completely and utterly lost. Must have tried 4 or 5 different approaches. And, well, now I do feel absolutely useless!!

    The task itself was, given 6 digits, create the earliest valid time in 24-hour format, and return as a string "hr:mm:ss", or return null if it's not possible with the digits provided.

    E.g.
    0,0,0,7,8,9 would return 07:08:09
    2,4,5,9,5,9 would return null.

    Can someone please perk me up by telling me this is not a rudimentary problem? In the meantime, I am consoling myself with the fact that the role would have entailed a bitch of a commute.

    I need to start using leetcode more!


«134

Comments

  • Registered Users Posts: 768 ✭✭✭14ned


    It's about six lines of C or Python code to implement. Both have standard library functions which do all the work. So I'd say a very rudimentary problem to solve.


  • Registered Users Posts: 568 ✭✭✭rgodard80a


    It does exercise the algorithm part of your brain alright.

    The simplest solution is to loop through all permutations with a variable holding the earliest date you can find, basically brute forcing the solution.

    Not very efficient, and there may be shortcuts to fail early by sorting the numbers ascending and fail if the first 2 can't make a number under 24 etc.

    But a lot of coding exercises are on loops, strings/array manipulation and maybe recursion so it's a typical exercise.

    At a minimum you could start learning the coding exercises, but really if you want to get into development you should be writing as much code as possible, your own pet projects, assignments, final year projects etc.


  • Registered Users Posts: 2,932 ✭✭✭Sniipe


    check out this website: https://www.codewars.com/

    It has loads of problems which are similar to what was asked. Try a few of them and start ranking up. You will get good at them in no time.


  • Moderators, Society & Culture Moderators Posts: 15,750 Mod ✭✭✭✭smacl


    rgodard80a wrote: »
    The simplest solution is to loop through all permutations with a variable holding the earliest date you can find, basically brute forcing the solution.

    Technically yes, but so hugely inefficient I wouldn't recommend using it in an interview. 86,400 iterations of a loop here. We've four intersecting sets of numbers to consider, 0-2 for first digit of 24 hour, 0-3 for second digit of 24 hour, 0-5 for first digit minutes and seconds, 0-9 for second digit of minutes and seconds. o(1) algorithm I'd go for is as follows

    Lowest digit of the time is the lowest digit in the input list
    If it is greater than 1 return fail
    Add it and remove it from list
    Second digit of the input time is the lowest digit in the list
    Add it and remove it from list
    If it is greater than 3 return fail
    Third digit of the input time is the lowest digit in the list
    If it is greater than 5 return fail
    If none of the three remaining numbers is less than six return fail
    If only one of the three remaining numbers is less than six
    Add lowest number as fifth digit and remove it from list
    Add lowest number as fourth digit and remove it from list
    Add lowest number as sixth digit and remove it from list
    Else
    Fourth digit of the input time is the lowest digit in the list
    Add it and remove it from list
    Fifth digit of the input time is the lowest digit in the list
    Add it and remove it from list
    Sixth digit of the input time is the lowest digit in the list
    Add it and remove it from list
    return success


  • Moderators, Society & Culture Moderators Posts: 15,750 Mod ✭✭✭✭smacl


    14ned wrote: »
    It's about six lines of C or Python code to implement. Both have standard library functions which do all the work. So I'd say a very rudimentary problem to solve.

    Lets see this in six lines of C please ;)


  • Advertisement
  • Registered Users Posts: 24,349 ✭✭✭✭lawred2


    smacl wrote: »
    Technically yes, but so hugely inefficient I wouldn't recommend using it in an interview. 86,400 iterations of a loop here. We've four intersecting sets of numbers to consider, 0-2 for first digit of 24 hour, 0-3 for second digit of 24 hour, 0-5 for first digit minutes and seconds, 0-9 for second digit of minutes and seconds. o(1) algorithm I'd go for is as follows

    Lowest digit of the time is the lowest digit in the input list
    If it is greater than 1 return fail
    Add it and remove it from list
    Second digit of the input time is the lowest digit in the list
    Add it and remove it from list
    If it is greater than 3 return fail
    Third digit of the input time is the lowest digit in the list
    If it is greater than 5 return fail
    If none of the three remaining numbers is less than six return fail
    If only one of the three remaining numbers is less than six
    Add lowest number as fifth digit and remove it from list
    Add lowest number as fourth digit and remove it from list
    Add lowest number as sixth digit and remove it from list
    Else
    Fourth digit of the input time is the lowest digit in the list
    Add it and remove it from list
    Fifth digit of the input time is the lowest digit in the list
    Add it and remove it from list
    Sixth digit of the input time is the lowest digit in the list
    Add it and remove it from list
    return success

    greater than 2 no?


  • Moderators, Society & Culture Moderators Posts: 15,750 Mod ✭✭✭✭smacl


    Pelvis wrote: »
    Can someone please perk me up by telling me this is not a rudimentary problem?

    I wouldn't say it is a rudimentary problem, I'd say it is a logic problem that demands a bit of thought than is immediately apparent to get an efficient solution. Don't be disheartened, do a couple of similar problems every few days for a couple of weeks and you'll get a better feel for them over a month or so.


  • Moderators, Society & Culture Moderators Posts: 15,750 Mod ✭✭✭✭smacl


    lawred2 wrote: »
    greater than 2 no?

    Well spotted. A bug!


  • Registered Users Posts: 24,349 ✭✭✭✭lawred2


    smacl wrote: »
    I wouldn't say it is a rudimentary problem, I'd say it is a logic problem that demands a bit of thought than is immediately apparent to get an efficient solution. Don't be disheartened, do a couple of similar problems every few days for a couple of weeks and you'll get a better feel for them over a month or so.

    to be honest I'm into my second decade as a developer and it's my plan this year to spend more time doing these challenges

    the development I'm doing is more procedural than computational/logical and the abstract thought aspect of development can get neglected

    plan is to do at least one challenge a day from coderbyte/leetcode/codewars

    get the auld brain ticking over


  • Moderators, Computer Games Moderators Posts: 4,281 Mod ✭✭✭✭deconduo


    smacl wrote: »
    Well spotted. A bug!

    You have a special case with the first 2 digits. 0-1 and 0-9, or 2 and 0-3


  • Advertisement
  • Moderators, Society & Culture Moderators Posts: 15,750 Mod ✭✭✭✭smacl


    lawred2 wrote: »
    to be honest I'm into my second decade as a developer and it's my plan this year to spend more time doing these challenges

    the development I'm doing is more procedural than computational/logical and the abstract thought aspect of development can get neglected

    plan is to do at least one challenge a day from coderbyte/leetcode/codewars

    get the auld brain ticking over

    Thirty years at it myself for my sins and do quite a lot of work with computational geometry which keeps the brain ticking over. Even then, I get things slightly wrong as often as not on the first go, as shown above. Never tried coderbyte/leetcode/codewars myself but sounds like the coders equivalent of the cryptic crossword which could be fun. Currently doing a course on deep learning which is giving the grey matter plenty to chew on.


  • Moderators, Society & Culture Moderators Posts: 15,750 Mod ✭✭✭✭smacl


    deconduo wrote: »
    You have a special case with the first 2 digits. 0-1 and 0-9, or 2 and 0-3

    And another bug, illustrating the value of code review and the importance of testing :P


  • Closed Accounts Posts: 1,758 ✭✭✭Pelvis


    I should say I didn't completely fail at this, I did pass a couple of test cases :D.

    But yeah, I think I messed up more in my approach. I didn't take time to think things through, I kind of just dove straight in thinking my first solution was right, only to realise it was completely wrong and having to start over.

    I've used codewars and hackerrank in the past, just occasionally. Leetcode seems to be all the rage now. Now that I've finally finished college and have some free time, I'll start using them a lot more to improve how I tackle these problems. As well as doing pet projects of course.


  • Closed Accounts Posts: 1,758 ✭✭✭Pelvis


    smacl wrote: »
    Technically yes, but so hugely inefficient I wouldn't recommend using it in an interview. 86,400 iterations of a loop here. We've four intersecting sets of numbers to consider, 0-2 for first digit of 24 hour, 0-3 for second digit of 24 hour, 0-5 for first digit minutes and seconds, 0-9 for second digit of minutes and seconds. o(1) algorithm I'd go for is as follows

    Lowest digit of the time is the lowest digit in the input list
    If it is greater than 1 return fail
    Add it and remove it from list
    Second digit of the input time is the lowest digit in the list
    Add it and remove it from list
    If it is greater than 3 return fail
    Third digit of the input time is the lowest digit in the list
    If it is greater than 5 return fail
    If none of the three remaining numbers is less than six return fail
    If only one of the three remaining numbers is less than six
    Add lowest number as fifth digit and remove it from list
    Add lowest number as fourth digit and remove it from list
    Add lowest number as sixth digit and remove it from list
    Else
    Fourth digit of the input time is the lowest digit in the list
    Add it and remove it from list
    Fifth digit of the input time is the lowest digit in the list
    Add it and remove it from list
    Sixth digit of the input time is the lowest digit in the list
    Add it and remove it from list
    return success

    Ignoring the bug someone else mentioned, this would fail for test case {0, 0, 0, 7, 8, 9}.


  • Moderators, Society & Culture Moderators Posts: 15,750 Mod ✭✭✭✭smacl


    Pelvis wrote: »
    Ignoring the bug someone else mentioned, this would fail for test case {0, 0, 0, 7, 8, 9}.

    Yep, you'd need to check you had available digits for first digit of hours and first of seconds before exhausting the set on second digits.


  • Registered Users Posts: 1,446 ✭✭✭Anjobe


    This problem is not so difficult. Put the six digits into an array and sort it in ascending order then:

    1.) If the 5th digit is greater than 5 then search back through the array from index 4 until you find a number that is not greater than 5. If you find one then swap that with the 5th digit, if not then return null.

    2.) If you had to do a swap in step 1 then re-sort the first 4 digits in the array.

    3.) If the 3rd digit is greater than 5 then repeat step 1, searching back through the array from index 2.

    4.) If you had to do a swap in step 3 then re-sort the first 2 digits in the array.

    5.) If the first digit is less than 2, or the first digit == 2 and the 2nd digit is less than 4 then you have a valid time and can copy the digits in order from the array to a string, inserting ':' after digits 2&4 and return your string, otherwise return null.


  • Registered Users Posts: 2,932 ✭✭✭Sniipe


    Pelvis wrote: »
    I need to start using leetcode more!

    I suggested codewars above, but I hadn't come across leetcode. I thought you were talking about coding in general. I must investigate. Thanks.
    This is an interesting task. I'm going to try it later on. Just curious - did you need the code to work (what language?) or was psudo code enough?


  • Closed Accounts Posts: 1,758 ✭✭✭Pelvis


    Sniipe wrote: »
    I suggested codewars above, but I hadn't come across leetcode. I thought you were talking about coding in general. I must investigate. Thanks.
    This is an interesting task. I'm going to try it later on. Just curious - did you need the code to work (what language?) or was psudo code enough?
    The code didn't have to work, I think they're more interested in the approach taken. Obviously passing test cases is not a bad thing though, but you only see a few test cases so you need to account for edge cases and the like.

    My attempt was using Java.


  • Moderators, Society & Culture Moderators Posts: 15,750 Mod ✭✭✭✭smacl


    Anjobe wrote: »
    This problem is not so difficult. Put the six digits into an array and sort it in ascending order then:

    1.) If the 5th digit is greater than 5 then search back through the array from index 4 until you find a number that is not greater than 5. If you find one then swap that with the 5th digit, if not then return null.

    2.) If you had to do a swap in step 1 then re-sort the first 4 digits in the array.

    3.) If the 3rd digit is greater than 5 then repeat step 1, searching back through the array from index 2.

    4.) If you had to do a swap in step 3 then re-sort the first 2 digits in the array.

    5.) If the first digit is less than 2, or the first digit == 2 and the 2nd digit is less than 4 then you have a valid time and can copy the digits in order from the array to a string, inserting ':' after digits 2&4 and return your string, otherwise return null.

    Still like to see it in six lines of C ;)


  • Registered Users Posts: 1,446 ✭✭✭Anjobe


    smacl wrote: »
    Still like to see it in six lines of C ;)

    Me too! My crude solution is about 60 lines of Java, which could probably be refactored into around half that if I could be bothered!


  • Advertisement
  • Registered Users Posts: 1,637 ✭✭✭victor8600


    smacl wrote: »
    Still like to see it in six lines of C ;)

    main() can be written in 1 line, there is no requirement to have line breaks in C. The remaining 5 lines can be used for #includes.


  • Registered Users Posts: 1,298 ✭✭✭off.the.walls


    i'm a few years in this now and I feel like I would of failed that test at a quick glance its a tricky question.

    And don't get too down about it is all i'll say, I did a test in my first year and completely failed that one, it's only 4 years later I found out the solution to that problem when I came across it in an article!


  • Registered Users Posts: 793 ✭✭✭ImARebel


    Anjobe wrote: »
    This problem is not so difficult. Put the six digits into an array and sort it in ascending order then:

    1.) If the 5th digit is greater than 5 then search back through the array from index 4 until you find a number that is not greater than 5. If you find one then swap that with the 5th digit, if not then return null.

    2.) If you had to do a swap in step 1 then re-sort the first 4 digits in the array.

    3.) If the 3rd digit is greater than 5 then repeat step 1, searching back through the array from index 2.

    4.) If you had to do a swap in step 3 then re-sort the first 2 digits in the array.

    5.) If the first digit is less than 2, or the first digit == 2 and the 2nd digit is less than 4 then you have a valid time and can copy the digits in order from the array to a string, inserting ':' after digits 2&4 and return your string, otherwise return null.

    I've been coding for 20 years yet even reading that solution made me nearly fall asleep.

    Believe or not there are jobs out there for coders who aren't into that type of coding. I know coz I am one :D

    Most of my coding is client sever business data and I've never had to make a time out of random numbers or anything like that challenge

    And I get by just fine :) don't give up!


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    ImARebel wrote: »
    I've been coding for 20 years yet even reading that solution made me nearly fall asleep.

    Believe or not there are jobs out there for coders who aren't into that type of coding. I know coz I am one :D

    Most of my coding is client sever business data and I've never had to make a time out of random numbers or anything like that challenge

    And I get by just fine :) don't give up!
    Collaborative coding tests are the interview method du jour. Virtually every role that has even a touch of coding in it, is now using some form of coding challenge in the interview process.

    They're mainly looking for the approach to the problem and the ability of the programmer to at least understand how to make their code faster and more reliable.

    Especially if you're going for "prestige" jobs in a big modern company (Google, Amazon, Facebook, etc), they're not just looking for programmers who can bang out working code by plugging frameworks together, they want programmers who understand how to design algorithms from the ground up and who have some semblance of understanding about performance, about sizes of data types, etc.

    This isn't a kind of wankey elitism. In the past when all software was something you deployed on-prem, then Oracle or whoever could get away with slow code and crap algorithms by just telling their customers to buy more hardware.

    In the new world of SaaS, all customers are equal, so you can't cover up ****ty code by loading the cost of it onto your customer. An algorithm that's half as fast, costs twice as much. So a programmer who understands why it matters whether an algorithm / data type is O(1), O(n), O(log n), etc., is less likely to take your system down than one who just loads a 2GB file into a variable and does a regex match on it.

    Of course, as you say, it does depend on the coder and their domain. If your expertise and interests are in coding integrations between systems, especially data exchange across networks, then there's a good chance that code performance is never going to be a significant factor given the relative slowness of the data interchange.

    For the OP, this isn't a difficult problem, but one I would have thought a bit on the "lateral thinking" side for a grad straight out of college. Definitely stick with the coding practice sites, and maybe get yourself a refresher book on the data structures and algorithms. My experience of college is that they flew through the theory in year one and then you never saw them again, everything else was practical coding.


  • Closed Accounts Posts: 1,758 ✭✭✭Pelvis


    When I have been doing these kind of challenges in my spare time, I do take into account time and space complexity. But as it happens, this particular challenge was only focused on correctness, they explicitly said they weren't evaluating performance.


  • Closed Accounts Posts: 22,649 ✭✭✭✭beauf


    ImARebel wrote: »
    ... I've never had to make a time out of random numbers or anything like that challenge...

    Converting or validating time/date, numbers especially from legacy systems, where they are strings, padded with spaces or unknown non visible characters, and can be different lengths, is pretty common for me.

    I actually enjoy solving these kind of problems more than just routine work. But I've never done them in a interview situation.


  • Moderators, Society & Culture Moderators Posts: 15,750 Mod ✭✭✭✭smacl


    seamus wrote: »
    In the new world of SaaS, all customers are equal, so you can't cover up ****ty code by loading the cost of it onto your customer. An algorithm that's half as fast, costs twice as much. So a programmer who understands why it matters whether an algorithm / data type is O(1), O(n), O(log n), etc., is less likely to take your system down than one who just loads a 2GB file into a variable and does a regex match on it.

    Yes and no. If the software service you're selling is charged based on CPU time, bandwidth and/or storage used, and if you're already as fast and lean as your competition or provide a unique solution, becoming leaner and faster simply reduces your income. Properly, SaaS should be priced based on results achieved but more often than not it is on resources consumed.


  • Posts: 17,378 ✭✭✭✭ [Deleted User]


    I thought it would be easier than it is. My excuse is I never studied this formally or worked in actual web dev. It's pretty damn ugly, uses too much cpu, and wouldn't get the job I guess.

    [HTML]
    var nums = [0,0,0,7,8,9];
    console.log(getTimes(nums));

    function getTimes(nums) {
    // Seconds
    var numbers = createNumbers(nums).reverse();
    for (var i = 0; i < numbers.length; i++) {
    if (!seconds && numbers <= 59) {
    if (numbers.split("").reverse().join("") < 59 && numbers.split("").reverse().join("") < numbers) {
    var seconds = numbers.split("").reverse().join("");
    } else {
    var seconds = numbers;
    }
    }
    }
    // Remove
    afterRemoved = removeNumbers(nums, seconds);
    // Minutes
    var numbers = createNumbers(afterRemoved).reverse();
    for (i = 0; i < numbers.length; i++) {
    if (!minutes && numbers <= 59) {
    if (numbers.split("").reverse().join("") < 59 && numbers.split("").reverse().join("") < numbers) {
    var minutes = numbers.split("").reverse().join("");
    } else {
    var minutes = numbers;
    }
    }
    }
    // Remove
    afterRemoved = removeNumbers(afterRemoved, minutes);
    // Seconds
    var numbers = createNumbers(afterRemoved);
    for (i = 0; i < numbers.length; i++) {
    if (!hours && numbers <= 23) {
    if (numbers.split("").reverse().join("") < 23 && numbers.split("").reverse().join("") < numbers) {
    var hours = numbers.split("").reverse().join("");
    } else {
    var hours = numbers;
    }
    }
    }
    if (hours && minutes && seconds) {
    return hours + ':' + minutes + ':' + seconds;
    } else {
    return null;
    }
    }
    function removeNumbers(nums, remove) {
    var first = false; var second = false;
    remove = "" + remove;
    // First digit
    for (var i = 0; i < nums.length; i++) {
    if (!first && nums == remove[0]) {
    nums.splice(i, 1); first = true;
    }
    }
    // Second digit
    for (var i = 0; i < nums.length; i++) {
    if (!second && nums == remove[1]) {
    nums.splice(i, 1); second = true;
    }
    }
    return nums;
    }
    function createNumbers(nums) {
    nums = nums.sort(function (a, b) { return a - b; });
    var numbers = [];
    // Loop first digit
    for (var i = 0; i < nums.length; i++) {
    // Loop second digit
    for (var j = 0; j < nums.length; j++) {
    // Ignore first digit
    if (i !== j) {
    numbers.push("" + nums + nums[j]);
    }
    }
    }
    var sorted = numbers.sort((a, b) => a - b);
    return sorted;
    }[/HTML]


    Try here: https://playcode.io/221316?tabs=console&script.js&output

    I might try and do it again some evening next week.


  • Registered Users Posts: 2,763 ✭✭✭accensi0n


    I don't work in development or a coding role.
    This is what I'd naively do in python (Done quickly with not much thought put in):
    I've probably missed something obvious.
    num = [0,0,2,2,5,6]
    num.sort()
    hour = str(num[0]) + str(num[1])
    min = str(num[2]) + str(num[3])
    sec = str(num[4]) + str(num[5])
    
    if int(hour) > 23:
        print("Time not possible from those numbers - hour problem")
    elif int(min) > 59:
        print("Time not possible from those numbers - minute problem")
    elif  int(sec) > 59:
        print("Time not possible from those numbers - second problem")
    else:
        print(hour + ":" + min + ":" + sec)
    


  • Advertisement
  • Posts: 17,378 ✭✭✭✭ [Deleted User]


    accensi0n wrote: »
    I don't work in development or a coding role.
    This is what I'd naively do in python (Done quickly with not much thought put in):
    I've probably missed something obvious.
    num = [0,0,2,2,5,6]
    num.sort()
    hour = str(num[0]) + str(num[1])
    min = str(num[2]) + str(num[3])
    sec = str(num[4]) + str(num[5])
    
    if int(hour) > 23:
        print("Time not possible from those numbers - hour problem")
    elif int(min) > 59:
        print("Time not possible from those numbers - minute problem")
    elif  int(sec) > 59:
        print("Time not possible from those numbers - second problem")
    else:
        print(hour + ":" + min + ":" + sec)
    

    Yeah, you've missed a lot. You'd get loads of unnecessary nulls and things like a minute figure of say 54 should then be changed to 45.

    What I wrote returns 00:22:56 for that.


Advertisement