3 kyu

How many are smaller than me II?

1,224 of 2,715joh_pot
Description
Loading description...
Algorithms
Performance
  • Please sign in or sign up to leave a comment.
  • Kloym Avatar

    This comment has been hidden.

  • parsamaghsoudi Avatar

    I've checked the multithreading code for this one,and still got the timeout error! Any suggestion for optimizing that better?

  • eillay Avatar

    This comment has been hidden.

  • carafelix Avatar

    This kata is kyu 2 for sure

  • eternal.white Avatar

    I'm a 100% sure I made a solution that's not O(n^2). I've tried it on my machine with a test case of list of 120K random numbers ranging from -1000 to 1000. The solution took about 14 seconds, and with the normal solution (from the standard kata, which is O(n^2)) it took more than 2 minutes.

    This kata is surely hard, I'll try to make it...

  • bloodpressure182120 Avatar

    everything was passed on the test, however when i attempt the code the results "STDERR Execution Timed Out (12000 ms)". and on the large_random_test was turned orange when there's no error on it.

  • OhanaTyan Avatar

    This comment has been hidden.

  • Miguel Pérez Avatar

    This comment has been hidden.

  • Cypholexias Avatar

    This comment has been hidden.

  • arpadkorossy Avatar

    I think I'm passing 66 (out of 100?) long tests before timing out with my current code after refactoring multiple times for greater optimization, but at this point I'm at a loss. I feel very close to getting in under the timeout threshold but I'm not sure where I can squeeze more efficiency. Any hints?

  • LexiWar Avatar

    Timed OutPassed: 67Failed: ?Exit Code: 1 Test Results: Log [4, 3, 2, 1, 0] [1, 1, 0] Fixed tests Initial_tests (7 of 7 Assertions) Completed in 0.59ms Random tests Small_random_tests (50 of 50 Assertions) Medium_random_tests (10 of 10 Assertions) Large_random_tests STDERR Execution Timed Out (12000 ms) Why did my code time out? Our servers are configured to only allow a certain amount of time for your code to execute. In rare cases the server may be taking on too much work and simply wasn't able to run your code efficiently enough. Most of the time though this issue is caused by inefficient algorithms. If you see this error multiple times you should try to optimize your code further.

  • AndrewMarsh Avatar

    This comment has been hidden.

  • TFish19 Avatar

    This comment has been hidden.

  • fanfois Avatar

    When I saw "Execution Timed Out (12000 ms)" I thought there was some quite heavy preformance requirements that my ocde did not met;

    But I have compared it with one of the accepeted solution (the starting with class Tree): on my PC my solution is 1.5x faster!

    I'm wondering if a more critical time limit on codewars server has now made this probleam unfeasible…

  • potomok Avatar

    How many bigsized tests are there in java version? I passed seven bigsized before timeout and I am thinking how close I am. Or is it better to try totally new algorithm?

  • KenMercusLai Avatar

    It seems it has been reported but hasn't been resolved.

    The test timedout even all tests passed.

    Large_random_tests
    Test Passed
    Test Passed
    Test Passed
    Test Passed
    Test Passed
    STDERR
    Execution Timed Out (12000 ms)
    
  • unwanted_feature Avatar

    This comment has been hidden.

  • kidCaulfield Avatar

    My JavaScript solution has a time complexity of 2 * O(n log n), so effectively O(n log n). On my laptop it's finishing 100000 element arrays in around 118ms when reporting it with console.time. I am getting through around 70 large tests at this time complexity, should O(n log n) be a passing solution?

    Update

    I refactored my solution and brought down the time complexity down to just a single O (n log n) algorithm.

    I cannot express enough how satifying it feels to finally complete this. This challenge provoked me to take an online course on data structures and algorithms. I have learned so much since I first attempted this challenge and stuck with it until I learned the proper data structure needed to complete it.

    In the end I was clocking around 12ms for a 100000 element array on my local machine. Tho the real accomplishment was what I learned in the journey to get there.

    Cheers and thanks for a great challenge!

  • Gretgor Avatar

    My solution is literally passing all random tests (50 small, 10 medium, 2 large) and still getting a timeout. How is that possible?

  • Bamboothesecond Avatar

    Something's not right here, my code just got passed in the test but I'm getting a time out prompt every time I submitting it

  • FoxX322 Avatar

    This comment has been hidden.

  • Mysza0105 Avatar

    Hello.

    I had a strange situation

    I got an information as below, and still got timed out. When I checked the console output, I treated 100 arrays of around 90000 elements each.

    Test Passed

    Completed in 11670ms

    Completed in 11686ms

    Completed in 11689ms

    STDERR

    Execution Timed Out (12000 ms)

    Why did my code time out? Our servers are configured to only allow a certain amount of time for your code to execute. In rare cases the server may be taking on too much work and simply wasn't able to run your code efficiently enough. Most of the time though this issue is caused by inefficient algorithms. If you see this error multiple times you should try to optimize your code further.

  • Mysza0105 Avatar

    Hello.

    How many large random tests am I supposed to compute?

    When I print the size of the input arrays I get a time out at about 72nd to 80th input array of about 90000 elements each.

    Update.

    I know, there are 100 large tests.

  • MikeLeonIT Avatar

    This comment has been hidden.

  • seth-noaa Avatar

    This comment has been hidden.

  • JarheadWeb Avatar

    This comment has been hidden.

  • patrickWarley Avatar

    This comment has been hidden.

  • Persa Avatar

    Nice kata. I am very satisfied with mine solution.

  • S.T.R.I.E.L.O.k Avatar

    I can pass 5 of 6 large test. I can only see one place to further optimazie my code. I thnink my time complexity is something like O(n + n*(1/2n)), Pice of code that i want to optimize is in coment below with a spoiler.

  • Hunter95 Avatar

    Let's say i have an algorithm that gives me the right array with time complexity of O(nlog(n)), more precise nlog(n)+2n. Maybe i can just optimize some instruction here and there, change the while statement with for...but i only pass the first 5 large test. Is there any hint about mathematical properties that i should care about? because i cannot think an algorithm more optimized than that, there must be some tricks, maybe with the structure/composition of the original array...pls help!

  • peasantCoder Avatar

    This comment has been hidden.

  • SciGeek Avatar

    My algorithm does a 20k array in 0.45 seconds. Why is it still slow?

  • Smooth Brain Avatar

    Is multithreading the solution ?

  • groskowski Avatar

    In python, even if I am not printing anything it runs out of time with a tree search algorithm. Worked in Java with minimal time

  • yoshaaa Avatar

    Maybe a performance tag?

  • LewisSaber Avatar

    This comment has been hidden.

  • user2787920 Avatar

    This comment has been hidden.

  • s-shutenko Avatar

    When I tried to get a long list to work on my device, I got an error Max Buffer Size Reached (1.5 MiB)

    My language is python

  • Avanta Avatar

    In Python, an incorrect answer for the large random tests will cause "Max buffer size reached" from printing. A custom assertion message should probably be used instead.

  • tomask272 Avatar

    This comment has been hidden.

  • QuinnZhao Avatar

    I have done with it in at least 3 ways, but I still got time out. I always stop at the time of testing large random test. So I want ask some helps here. Could you please show me your hands?

  • FoxX322 Avatar

    This comment has been hidden.

  • AdielTzubery Avatar

    This comment has been hidden.

  • prjavelin Avatar

    This comment has been hidden.

  • alexeyten Avatar

    There is something wrong with tests in JavaScript. Trivial (and obviously wrong) code timed out. It shouldn't pass any test, but it must not time out.

    function smaller(arr) {
      return [];
    }
    
  • Awesome A.D. Avatar

    Rust translation ready for review

  • karumaido Avatar

    This comment has been hidden.

  • ecolban Avatar

    This comment has been hidden.

  • cafalchio Avatar

    I am passing all the tests, but still getting time error :/ seems strange.

    Random tests Small_random_tests (50 of 50 Assertions) Medium_random_tests (10 of 10 Assertions) Large_random_tests (4 of 4 Assertions)

  • cafalchio Avatar

    This comment has been hidden.

  • Ghait Avatar

    I already have 3 different working solutions, but all of them are too slow (timing out).... kill me please :D

  • Blind4Basics Avatar

    python: "optimized" O(N²) solutions are passing. That shouldn't happen, afaik.

    ideas:

    • worst case scenarii could be tested with big arrays (easy to generate)
    • input generation could be reduced by generating one single list and randomizing it between calls (or only a significant bunch of elements, maybe)
  • Blind4Basics Avatar

    the following test should be added in all languages (this case doesn't appear in any fixed test):

    test.assert_equals(smaller([5, 4, 7, 9, 2, 4, 1, 4, 5, 6]), [5, 2, 6, 6, 1, 1, 0, 0, 0, 0])
    
  • akar-0 Avatar

    This comment has been hidden.

  • george.arnoldrex Avatar

    This comment has been hidden.

  • bruschmaker Avatar

    This comment has been hidden.

  • PetervidaHU Avatar

    Just give me a hint. Is there any special mathematical-type solution?

  • gulamov_nuriddin Avatar

    How can I deal with large random tests? (JavaScript)

  • lociero Avatar

    This comment has been hidden.

  • miguelh72 Avatar

    This comment has been hidden.

  • mokuj1n Avatar

    My Segment Tree solution(javascript) is not fast enough, pls give me some tips.

  • wowthecoder Avatar

    I used binary search and it's not fast enough...can someone please give me some tips on how to improve? Should I try interpolation search? Maximum amount of test cases I can pass is 160 (100 medium size + 6 initial tests + 54 large size) I am using Java by the way.

  • Sabri0o Avatar

    Hi, I am running an O(log(n)) algorithm inside a loop, if i am not wrong the resulting time complexity should be O(nlog(n))

    var smaller = function(nums) { first helper function here O(nlog(n))) loop{ second helper function here O(log(n)) } }

    if so why am i getting execution timed out(12000 ms) issue. for the small random tests (100 of 100 Assertions) tests were completed in 70ms i could pass only 4 of 4 (large random tests) any advice would be appreciated

  • j0nl1 Avatar

    I'm a bit confused about this kata. I always get Execution Timed Out (12000 ms) but it's weird because when I check the tests I get 'Small random tests (100 of 100 Assertions)' in 'Completed in 102ms' I don't know if my algorithm is not fast enought or there's a issue with the tests.

    Javascript version

  • xromatron Avatar

    This comment has been hidden.

  • Aminsay Avatar

    Good unique kata

  • everas Avatar

    This comment has been hidden.

  • Ciprian Amza Avatar

    Python language is not available?

  • dawidszewczyk Avatar

    This comment has been hidden.

  • cavca2012 Avatar

    Had somebody made it in Java?

    Because I already have an O(n log n) algorithm and the output still been "Execution Timed Out (16000 ms)"

    Just tell me yes or no...to change of lenguage or to try harder :)

  • Saleem07 Avatar

    I wrote two solutions both of them have this exit code problem but the output is proper. any tip or suggestion is welcomed.

  • JorgeVS Avatar

    This solution is exactly the same solution as the reference solution, even the comments are the same. This must be a very big coincidence ...

    Only //what? is added to not get a copy solution.

    More equal solutions ...

    This one and this one.

    Another one:

    This one and this one.

  • hilechen Avatar

    Very cool kata! Super satisfied once solved. It's one of the rare katas I've seen that forces using a better data structure.

    Thank you author!

  • YakindaQariban Avatar

    This comment has been hidden.

  • JorgeVS Avatar

    Java translation is ready for review.

  • wojtekweg Avatar

    The Instructions should contain the information about time complexity. I can only guess that quadratic solutions are not acceptable, imo that should be said at the beginning

  • BooleT37 Avatar

    My solution passes 193 tests and then times out. Just how many more are out there??

  • Alfabetix Avatar

    The second description example is wrong, should be an array and instead it is 3 numbers

    smaller([5, 4, 3, 2, 1]) === [4, 3, 2, 1, 0]
    -->smaller(1, 2, 0) === [1, 1, 0]
    
  • ShehabMuhammad Avatar

    I didn't understand the instructions, Can some one explain further?

  • VadymKush Avatar

    This comment has been hidden.

  • OreOlad@ Avatar

    Random tests are way too big. I've done 7 different approaches to the answer in Javascript, two in ruby, and three in crystal, and all of them pass the first 106 tests, then they time-out on the random tests. It'd be nice if you could remove them or make them shorter, because at this point I'm not sure algorithm efficiency is a problem.

  • Alexandr Semizbayev Avatar

    This comment has been hidden.

  • FArekkusu Avatar

    Array is too large to print out - Expected: 'true', instead got: 'false'

    There must be smaller tests too, otherwise it's impossible to debug the solution.

  • Chrille Avatar

    Is there some way to remove my solution? It is not my solution, I copied it from the solutions on the easy cata just to see if it would pass (attempt only, not submit) it but I did not thought it was gonna get submitted.

  • 00dev Avatar

    PHP translation is ready. Please, review and approve

  • user8436785 Avatar

    This comment has been hidden.

  • kolibox Avatar

    There is an obvious way of two cycles, but it has n^2 difficulty. Give me a hint how to reduce the diffuculty, please.

  • dmercertaylor Avatar

    It might be nice to get some moderate size random tests (maybe array size 25 or so), since there are many cases which might pass the static tests but fail random due to logical errors, not timeouts. As it is, it's almost impossible to actually debug in the test suite without just writing my own random tests.

  • roelofza Avatar

    This puzzle is throwing me a bit, any hints?

  • ALowVerus Avatar

    This comment has been hidden.

  • Firefly2002 Avatar

    Ruby translation submitted

  • tiagosalema Avatar

    Is it really possible to solve this kata in <12s? I thought my algorithm was quite effective, but apparently not 😅 as it times out...

    Tried running smaller = x => null and it takes 6+ seconds to execute, so there's like ~6 sec to execute the algorithm on all the test cases.

    Help?

  • laynef Avatar

    This comment has been hidden.

  • DegoyDegoy Avatar

    This comment has been hidden.

  • Jomopipi Avatar

    this is a hard one for me... I humbly ask: any advice?

  • kodejuice Avatar

    This comment has been hidden.

  • metalim Avatar

    Seeing how many times same questions are being asked again and again, posting a conclusion:

    1. there are 100 random tests with very large arrays. Tests do work.
    2. printing test inputs will lead to timeouts, as arrays are large.
    3. if you timeout on first random test, then your solution approach is wrong. Try something else.
    4. if you pass some of random tests, then you're (probably) thinking in the right direction. Continue on.
    5. reference solution takes around 6 seconds, leaving 6 seconds (from global 12s limit) for your solution. It you timeout at 90+ tests, you can just retry. Due to nature of random tests they take different amount of time.
  • metalim Avatar
  • metalim Avatar

    This comment has been hidden.

  • HugoHub Avatar

    This comment has been hidden.

  • HugoHub Avatar

    Sadly I find this kata near imposible without more hints :(

  • Orbry Avatar

    Wow. Just wow. My initial attempt didn't even passed first random test, so after some good thought and optimisation I've managed to beat it, even passed 2 random tests before timeout, but got stuck on third. Then, out of curiosity I've tried to run random test with dummy function, just to know how much tests are there, was pretty sure third would be the last one, but just in case... So after I've picked up my jaw from chair, and seeing that just test code was eating about 8 seconds from 12, I was pretty sure that test case is bugged, glad I've checked discussions. Now I slowly realise the amount of optimisation required, the whole approach must be wrong, though got a hint from post down here - accesing object properties is slow, hmm... Got to think now...

  • with-a-k Avatar

    This comment has been hidden.

  • tmine Avatar

    This comment has been hidden.

  • alanolmo Avatar

    Hi everyone again! Thanks a lot for help with this issue, I found that I had a very stupid error in implementation (no idea how it passed sample tests :)).

    Now I wonder - is it OK that I have "Array is too large to print out - Expected: 'true', instead got: 'false'" messages? (I'm definatelly not trying to print anything - I run empty function - maybe it's some issue with test? Should I ignore it and continue coding?) And the second question to people who passed this kata - is it OK that when a try to run empty function it takes 7-8seconds or it's caused somehow by that messages about array size?

  • tmine Avatar

    hmm.. i have two algorithms, one takes about 1380ms and the other only 80ms.. but the random tests still fail. I think it would be much better to give feedback in the random tests and tell which testcase failed. With the current test suite it is very unhelpful to figure out what is missing/wrong with the solution. (Unless this is not even about the performance but about writing a new testsuite to debug the code?)

    In addition it would be nice to give a hint in the description what is actually the problem to solve (performance) and give clear boundaries for the maximum execution time.

  • alanolmo Avatar

    This comment has been hidden.

  • dcbrwn Avatar

    This comment has been hidden.

  • rasheed72 Avatar

    The description should be more specific of what this kata asks for. Statements should be added such as:

    The kata test cases have 100 random tests with arrays with between 80k-100k elements in them. A simple solution might take hours to finish the tests, but this kata requires them to be done in less than 12 seconds. The fastest solutions take around 10 seconds to finish.

    Specfically explaining what the kata requires, rather than just calling it "a hard version" would allow users to make an informed decision on whether or not to attempt to solve it.

  • cypherfunc Avatar

    This comment has been hidden.

  • RobinKnipe Avatar

    Your tests don't complete in time! Please refactor your "random test" suite.

  • halcarleton Avatar

    This comment has been hidden.

  • claimred Avatar

    This comment has been hidden.

  • rscharfer Avatar

    This comment has been hidden.

  • dsimmons87 Avatar

    This comment has been hidden.

  • Alexandroppolus Avatar

    This comment has been hidden.

  • Voile Avatar

    ...Okay, so I somehow broke the kata accidentally by first trying to return arr.length for the random test cases because I wanted to see their sizes. See my solution. In fact, returning anything related to arr works: arr, arr[0], arr[100], ...

    Something is definitely broken but I can't figure out where, probably related to the Function.prototype.toString override. It's pretty weird, and probably bad because it causes arrays to not behave like how it is typically at all.

  • smile67 Avatar

    I just changed your testcases a little bit and approved the kata (average was 3kyu), as expected many solutions got invalid. It's older than one year and it really was (is;-)) a great kata. So hope it's ok for you?

  • PaulCalvelage Avatar

    The following line of code in the (hidden) preloaded section is causing some weird behavior:

    Function.prototype.toString = {};
    

    Effectively it is preventing the use of typed arrays for some reason that I do not comprehend. Really it shouldn't do that, so I filed an issue against Codewars itself as well. See https://github.com/Codewars/codewars.com/issues/279.

    In the meantime, I suggest working around the issue, perhaps by changing it to return an empty string:

    Function.prototype.toString = function() { return ''; }
    
  • smile67 Avatar

    This comment has been hidden.

  • ScottKaye Avatar

    This comment has been hidden.

  • Hacker Sakana Avatar

    This comment has been hidden.

  • smile67 Avatar

    PS: Very nice kata, really hard i think (or i'm blind for a quick solution - the first (but very short) one breaks after ~ 50 random tests);-)! 10$ for reducing to 40 random tests:-)?!

  • myjinxin2015 Avatar

    How many random tests are there? my solution only pass 6 basic + 16 random ...

  • Jotha Avatar

    At first, I'm not a professional in javascript, but I tried several ways and was't able to pass the random tests. What a pitty! May someone cangive me a hint on which way I should concentrate:

    • simple counting the values which are smaller
    • using Array.map and Array.filter
    • sorting the array and using the index of current value

    As you can see, I'm a little desperated...

  • Jotha Avatar

    Little typo in the tag section peformance --> performance.

    Futhermore, I would add a sentence to the description saying that this kata is exactly the same like your first kata, but there are more complex tests which concentrate on performance issues.