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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Disheartened

  • 16-01-2019 10: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!


«1

Comments

  • Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 2,933 ✭✭✭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,812 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,812 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, Registered Users 2 Posts: 24,559 ✭✭✭✭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,812 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,812 Mod ✭✭✭✭smacl


    lawred2 wrote: »
    greater than 2 no?

    Well spotted. A bug!


  • Registered Users, Registered Users 2 Posts: 24,559 ✭✭✭✭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,282 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,812 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,812 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,812 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, Registered Users 2 Posts: 1,452 ✭✭✭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, Registered Users 2 Posts: 2,933 ✭✭✭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,812 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, Registered Users 2 Posts: 1,452 ✭✭✭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, Registered Users 2 Posts: 1,681 ✭✭✭Apiarist


    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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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,648 ✭✭✭✭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,812 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, Registered Users 2 Posts: 2,887 ✭✭✭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.


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,125 Mod ✭✭✭✭AlmightyCushion


    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)
    

    That wouldn't work for 0,0,0,7,8,9. It would say the time is not possible when it is. 07:08:09


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


    Seamus wrote:
    Now you don't need to loop through the input array a number of times, you only need to loop once.

    You've just unrolled a small loop there, many if not most optimizing compilers will do this for you already.
    However, the big bottleneck in the above code is sorting the input to begin with

    Sorting single digit numbers is an O(n) counting sort, so not actually a bottleneck in this case

    Edit: Quoted post vanished!


  • Registered Users, Registered Users 2 Posts: 1,452 ✭✭✭Anjobe


    smacl wrote: »
    You've just unrolled a small loop there, many if not most optimizing compilers will do this for you already.



    Sorting single digit numbers is an O(n) counting sort, so not actually a bottleneck in this case

    Edit: Quoted post vanished!

    I don't think it worked!


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


    Anjobe wrote: »
    I don't think it worked!

    Possibly not, but I think the principle that you can often invest more time to get a better performance holds well enough.


  • Registered Users, Registered Users 2 Posts: 793 ✭✭✭ImARebel


    seamus wrote: »
    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.

    i totally get your point and I agree the likes of Google and Amazon do expect that sort of knowledge.

    But the OP was feeling disheartened, I was just saying there is life beyond algorithms if they aren't his thing. I was just giving a different view point.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 793 ✭✭✭ImARebel


    beauf wrote: »
    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.

    yeah me too but no one has ever pulled a random set of 6 digits from a DB and asked me to make a time out of it

    it already is a time, albeit in whatever format and you're then transforming that data. I do plenty of that too

    I just don't conjure up time out of a set of numbers someone has plucked out of their ass, the data I work with has meaning. there's only so many ways hr:mm:ss can be stored in any DB whether legacy or otherwise. i'm yet to see 1:05am in the morning being recorded as {1,2,0,5,9,7} (or whatever) and I've to deduce it from a set of 6 numbers

    I was just giving a point of view that if algorithms aren't his thing there are other skillsets out there being utilised by coders everywhere.


  • Registered Users, Registered Users 2 Posts: 1,452 ✭✭✭Anjobe


    smacl wrote: »
    Possibly not, but I think the principle that you can often invest more time to get a better performance holds well enough.

    Of course that's true, however I think this exercise was more to test the candidate's ability to analyse a problem and design an algorithm to solve it. There is not much point spending time optimising code for performance if you don't understand what it does and break the functionality.


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


    Anjobe wrote: »
    Of course that's true, however I think this exercise was more to test the candidate's ability to analyse a problem and design an algorithm to solve it. There is not much point spending time optimising code for performance if you don't understand what it does and break the functionality.

    Very true, but if the OP were to come up with an overly slow brute force solution they'd likely get marked down for it.


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


    smacl wrote: »
    Edit: Quoted post vanished!
    Anjobe wrote: »
    I don't think it worked!
    Nope! :D

    It would have nearly worked, but nearly isn't good enough. I was too focussed on on the array specifics, I missed the edge case.


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


    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.
    Anjobe wrote: »
    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!

    This sounds good! So figured I'd give it a try. Here's my effort, it seems to work well. Anyone else want to try a few more test cases to confirm?
    import java.util.Arrays;
    import java.util.HashMap;
    
    class Solution {
    
        public static String arrayToTime(int[] A) {
    
            Arrays.sort(A);
            int len = A.length;
    
            HashMap<Integer, Integer> maxNum = new HashMap<>();
            maxNum.put(0, 2);
            maxNum.put(1, 3);
            maxNum.put(2, 5);
            maxNum.put(3, 9);
            maxNum.put(4, 5);
            maxNum.put(5, 9);
    
            for (int i = len-1; i >= 0; i--){
    
                if (i == 1 && A[i] > 3 && A[i-1] == 0) break;
    
                int cur = A[i];
                if (cur <= maxNum.get(i)) continue;
                else {
                    for (int j = i-1; j >= 0; j--){
    
                        if (A[j] <= maxNum.get(i)){
                            int tmp = A[j];
                            A[j] = A[i];
                            A[i] = tmp;
                            Arrays.sort(A, 0, i);
                            break;
                        }
                    }
                }
            }
    
            if (A[0] > 2 || (A[1] > 3 && A[0] != 0 )){
                return null;
            }
    
            StringBuilder time = new StringBuilder(8);
            for (int i=0; i<A.length; i++){
                if (time.length() == 2 || time.length() == 5){
                    time.append(":");
                }
                time.append(A[i]);
            }
            return time.toString();
        }
    
        public static void main(String[] args) {
            System.out.println(arrayToTime(new int[] {0, 0, 0, 7, 8, 9}));
            System.out.println(arrayToTime(new int[] {1, 8, 3, 2, 6, 4}));
            System.out.println(arrayToTime(new int[] {2, 4, 5, 9, 5, 9}));
            System.out.println(arrayToTime(new int[] {7, 2, 4, 1, 9, 4}));
            System.out.println(arrayToTime(new int[] {8, 4, 7, 1, 8, 6}));
            System.out.println(arrayToTime(new int[] {8, 9, 0, 4, 0, 0}));
        }
    }
    

    I'm still of the opinion that this is pretty tough for an entry level graduate role!!


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 7,521 ✭✭✭jmcc


    Pelvis wrote: »
    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.
    If you wanted to mess with their minds, you could have asked if the digits individually represented a time. :) It could have been one of those tests to see how you think.

    Regards...jmcc


  • Registered Users, Registered Users 2 Posts: 203 ✭✭Sherfin


    My attempt

    Sort numbers and place into 2 arrays
    Under6[]
    Over5[]

    If Over5.count > 2 then fail

    If (Under6[0] > 2 or Under6[1] > 3) then fail

    1st = Under6[0]
    2nd = Under6[1]
    3rd = Under6[2]
    If Over5.count > 1
    4th = Over5[1]
    else
    4th = Under6[3]
    5th = Under6[4]
    If Over5.count > 0
    6th = Over5[0]
    else
    6th = Under6[5]


  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    Fails because should be over4 and under5, and a bunch of other stuff :D


  • Registered Users, Registered Users 2 Posts: 152 ✭✭Razzen


    try this...

    see if you can work out the logic..
    public String solve(int A, int B, int C, int D, int E, int F) {
      int[] d = {A, B, C, D, E, F};
      Arrays.sort(d);
      if (d[4] < 6) { // 2nd largest digit is smaller 6, we can just fill up
        if (10 * d[0] + d[1] < 24)
          return "" + d[0] + d[1] + ":" + d[2] + d[3] + ":" + d[4] + d[5];
        else
          return "impossible";
      } else if (d[3] < 6) { // 3rd largest digit is smaller 6, put 2nd largest in 4th position
        if (10 * d[0] + d[1] < 24)
          return "" + d[0] + d[1] + ":" + d[2] + d[4] + ":" + d[3] + d[5];
        else
          return "impossible";
      } else if (d[2] < 6) { // 4th largest digit is smaller 6, put 3rd largest in 2nd position
        if (10 * d[0] + d[3] < 24)
          return "" + d[0] + d[3] + ":" + d[1] + d[4] + ":" + d[2] + d[5];
        else
          return "impossible";
      } else {
          return "impossible";
      }
    }
    


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


    Slightly off but still on topic. I have an opportunity to move to a dev role in my current company, it's in the early stages of an expansion and there are going to be plenty of new systems built in-house.

    One the one hand, I was kind of eager to leave once I graduated, as I've been there a long time and wanted a fresh start. On the other hand, I wouldn't have to take the wage cut I would have taken had I moved to a grad role elsewhere.

    Another con, I think, is that it's a PHP role, where as I would prefer to work with Java. I'm thinking more long term here, as don't PHP devs earn less on average? If I were to take it would I be pigeonholed as a PHP dev?


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


    Only if you don't keep working on your skillset in other areas. Which you should.

    You get to keep your salary and do some dev work. Do other stuff on the side . I'm not seeing a downside.


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


    ImARebel wrote: »
    yeah me too but no one has ever pulled a random set of 6 digits from a DB and asked me to make a time out of it

    ...

    Lucky you : )

    I take your point that there are lots of other development jobs. Though most programmers have a very narrow definition of what programmer is and isn't.

    Still this has been one of the more interesting threads in this forum in a while. I find the discussion from very experienced Devs interesting especially around performance.

    Where I am no one would be interested in having a discussion like and not with lesser mortals like myself.


  • Registered Users, Registered Users 2 Posts: 1,029 ✭✭✭John_C


    Pelvis wrote: »
    Slightly off but still on topic. I have an opportunity to move to a dev role in my current company, it's in the early stages of an expansion and there are going to be plenty of new systems built in-house.
    In your current company you'll have domain knowledge (i.e. you know the ropes) which you won't have in a new job. That makes you more valuable than another developer with similar skills.

    My advice is to stay, assuming that you enjoy working there and that there'll be experienced developers for you to learn from.


  • Registered Users, Registered Users 2 Posts: 768 ✭✭✭14ned


    smacl wrote: »
    Lets see this in six lines of C please ;)

    time_from_digits() could be compressed into less than six lines, but it's easier to read this way:
    #include <time.h>
    #include <limits.h>
    #include <stdio.h>
    
    int digit(const char *i, int limit)
    {
        int ret=(i[0]-'0')*10+(i[1]-'0');
        return (ret>=limit) ? -1 : ret;
    }
    
    const char *time_from_digits(const char *input)
    {
        struct tm t{digit(input+4, 60), digit(input+2, 60), digit(input+0, 24), 1, 1, 1970, 0, 0, 0, 0, 0};
        if(t.tm_sec<0 || t.tm_min<0 || t.tm_hour<0 || mktime(&t)<0) return "null";
        char *r=asctime(&t)+11;
        r[8]=0;
        return r;
    }
    
    int main(void)
    {
        printf("&#37;s\n", time_from_digits("122304"));
        printf("%s\n", time_from_digits("000000"));
        printf("%s\n", time_from_digits("235960"));
        printf("%s\n", time_from_digits("240100"));
        return 0;
    }
    

    Obviously this is a terrible implementation which nobody should use, but the key is to make use of the standard library functions to do the validation and printing of times. The reason we need to clamp the values by hand is because asctime() is too clever, and automagically "repairs" invalid input silently on your behalf. Any language which is not C would not do such a stupid behaviour, so the above would be much shorter and simpler on say Python, or even C++.

    You can see the above running correctly at https://wandbox.org/permlink/5p4EqctOdfFx8YAh

    Niall


  • Registered Users, Registered Users 2 Posts: 1,452 ✭✭✭Anjobe


    14ned wrote: »
    time_from_digits() could be compressed into less than six lines, but it's easier to read this way:
    #include <time.h>
    #include <limits.h>
    #include <stdio.h>
    
    int digit(const char *i, int limit)
    {
        int ret=(i[0]-'0')*10+(i[1]-'0');
        return (ret>=limit) ? -1 : ret;
    }
    
    const char *time_from_digits(const char *input)
    {
        struct tm t{digit(input+4, 60), digit(input+2, 60), digit(input+0, 24), 1, 1, 1970, 0, 0, 0, 0, 0};
        if(t.tm_sec<0 || t.tm_min<0 || t.tm_hour<0 || mktime(&t)<0) return "null";
        char *r=asctime(&t)+11;
        r[8]=0;
        return r;
    }
    
    int main(void)
    {
        printf("%s\n", time_from_digits("122304"));
        printf("%s\n", time_from_digits("000000"));
        printf("%s\n", time_from_digits("235960"));
        printf("%s\n", time_from_digits("240100"));
        return 0;
    }
    

    Obviously this is a terrible implementation which nobody should use, but the key is to make use of the standard library functions to do the validation and printing of times. The reason we need to clamp the values by hand is because asctime() is too clever, and automagically "repairs" invalid input silently on your behalf. Any language which is not C would not do such a stupid behaviour, so the above would be much shorter and simpler on say Python, or even C++.

    You can see the above running correctly at https://wandbox.org/permlink/5p4EqctOdfFx8YAh

    Niall

    I think you have missed the point that you may need to rearrange the digits in the input in order to make a valid time, or the earliest time. Your program returns null for the basic test case 000789, and the earliest time that can be made from the digits 122304 is 01:22:34.


  • Advertisement
Advertisement