Unofficial Solutions to the Training by Codility is another great place to improve our programming skills. Besides challenge questions, it also provides some training problems here. Train myself, and record here.

Lesson 1: Iterations

Lesson 2: Arrays

Lesson 3: Time Complexity

Lesson 4: Counting Elements

Lesson 5: Prefix Sums

Lesson 6: Sorting

Lesson 7: Stacks and Queues

Lesson 8: Leader

Lesson 9: Maximum Slice Problem

Lesson 10: Prime and Composite Numbers

Lesson 11: Sieve of Eratosthenes

Lesson 12: Euclidean Algorithm

Lesson 13: Fibonacci Numbers

Lesson 14: Binary Search Algorithm

Lesson 15: Caterpillar Method

Lesson 16: Greedy Algorithms

Lesson 17: Dynamic Programming

Lesson 90: Tasks from Indeed Prime 2015 challenge

Lesson 91: Tasks from Indeed Prime 2016 challenge

Lesson 99: Future Training

Previous Challenges:

Interview Questions (Following the request from Codility, all the questions & solutions are removed.):

  • Count Arithmetic Sequence: REMOVED
  • Tree Amplitude: REMOVED
  • Best Shuffle: REMOVED

52 Replies to “Unofficial Solutions to the Training by Codility

  1. First of all, thanks a lot for sharing the solution!
    I deleted some duplicate comments from you. If you want to post the code as comments, you need to replace all the greater or smaller symbols: for less than < use “&lt;”. For greater than >, use “&gt;”
    In terms of your posted code, could I include it in my future post with you name? I would like to remove it from comments section to make it clear. Looking forward to your reply!

    • Yes Sheng, remove it with from the comments. You could add it in your post with my name.
      it is important also to correct #include as I told you
      minor_or_equal_to and greater_or_equal_to. If you want, You could send me an email, I could email the code to avoid this posting problems
      I Have C++ solutions for a lot of codility test, all the lesson + some of futher training, I could share it with you. only acknowledgment
      of my name is needed.
      I think that you are doing a great contribution to the community, I want to contribute also,

      • Since you have done a lot of works on these lesson, and are willing to share them, why do not you open a GitHub or blog to share them? It is good for all, including others and yourself.
        BTW, I will make the posts with your two solutions in days. And thanks again for your sharing!

  2. Hi there,
    could you *open source* these on Github so we can add solutions from other langues?
    Make sure you only accept code that scores 100% only 🙂

    • This is really a good idea! Wisdom of crowd is surely better than mine. Sometimes, the readers posted their 100/100 solutions among the commments. And I realized I cannot provide the solution in every single programming language. Only a team of olunteers could do it.
      But I also noticed that, GitHub is not convenient to discuss. So I continue to post the solutio here. I might be wrong, because I did not use Github much.
      Do you think it is really a good place to do this?

    • Also I tried not to use the specific features of Python. So that every programmer, no matter what the programming language is used, should be able to understand the process.

  3. FWIW, I don’t see much value in such an initiative. These exercises are pretty much language-independent. I’d go even further and say that they are not good exercises to learn a given programming language, but more for generic programming techniques. You won’t be learning any object-orientation in Java, for example, or about memory management in C/C++. I know nothing about Python, but Sheng’s solutions seem pretty readable to me, it’s basically pseudo-code…

  4. What I’d expect *Codility* to do, on the other hand, is to set up a wiki so that developers can perfect the various solutions and explanations. Or do the same as with their challenges, post an official solution.

    • Codility does post some official solutions, but not for all. They are a business company, and have to pay lots of time on the business things. Even thought, codility is the best online judge system with free training, I’ve ever used.
      In addition, are there enough users to build a good and active wiki? If there are, some users could build the wiki by theirselves.

  5. Hi, I hope you can continue to follow the challenges of codility. There’re too less information for the latest codility challenge’s solution. Thanks.

  6. Hi Sheng,
    I completed the problems here. Just wanted to thank you so much for your website. It truly helped me to learn. Special thanks to your replies on my comments.

  7. Codility Dynamic programming lessons are missing from this collection. Check here
    I am posting the solution of NumberSolitaire.

    • try with:
      [-8828, -8878, 6784, -1046, 7076, -6104, 9357, 614, 4013, 9237, 8167, -5558, 7971, -9795, -4693, -4839, -6899, -5279, -4476, -4947]

      • Thanks for discussion (I DID NOT check whether SA or alexey is correct.)! I will add the solutions for the new lessons when I am free.

  8. I believe the test are not fair . Because to pass the actual test person must complete all the practice test .It extremely hard to pass the test without practice . So when potential employer sends you this test you have maximum 1-2 day to complete the test .Which mean person who practice has advantage . So I suggest to stop this outrage and create a counter web page with all the solutions . I can get all the question buy buying the access to the tests . Then I will post the questions on the web page and we all create solutions public . When the all solutions became public we will constantly check for new questions and post it on web site . In the end we will have all the solutions in one place published in nice searchable format (ctrl+f on web page).
    The only way to destroy this company is make them write constantly new questions .
    This company trays to blackmail people who look for the job to play by their rules and pay them money. I would like to know if I am the only one who files this way .

    • Sorry I cannot agree with your proposal. But I do agree that it is not completely fair to judge one only by OJ. Operating System, network, database, etc. are also extremely important in software engineering.

  9. But problem with Codility is that you cannot import and you need to write everything from scratch?

    • Yes, I agree it is inconvenient. However, except heap, the default library is nearly enough for all the challenges.

        • Heap is included in the Python standard library, but not build-in. We need to import it, which is forbidden by Codility.

          • To get a Max heap, we could:
            1. Re-write the __cmp__ function of the object. It is similar with the custom Compare method in Java.
            2. Flip the value of the object. For example, to store number 9, we can store -9 instead. And when we retrieve the content from the heap, we transfer it to the original value. In this way, we can use the min-heap as max-heap.

  10. Do you need to write Exceptions in order to pass the test inside the code, since the left hand corner also has up to 5 Test cases. Or is that optional?

  11. But I think somewhere I saw in Codility, you can import inside a function?
    def myFunction():
    from math import random
    but not on top level import?

  12. My solution pythonic of MinAvgTwoSlice in O(n) got 100%:

  13. Thanks for this great resource and for providing the solutions to Codility’s problems. I have an online test coming up soon with them and this has helped me loads.

    I have also found a great udemy course on these problems in Python, Java and Ruby:

  14. Hi Sheng! Thanks for the very elegant answers.
    This is my answer of ThreeLetters from the “Coding Skills” section:

  15. My C++ solution of CountConformingBitmasks from the Bitwise operations (bit-ops) section:

  16. This is my implementation CountBoundedSlices using a segment tree:
    It achieved 100% score and time complexity O(N) (although it is O(N log N) indeed).
    This code is a little bit C++’tonic but I think that one can easily port it to python if necessary.

  17. This is my C++ solution for the CountriesCount (Data Structures section):

  18. This is my C++ solution for DisappearingPairs (Algorithms Skills section)
    This solution uses a custom double linked list which can remove nodes even if they are inside the list, achieving 100% and O(N)
    I believe that it is possible to solve it in a simpler way though. Any suggestions are welcome.

    • C’mom I just found a way simpler solution using a single built in stack:

Leave a Reply to Anonymous Says Cancel reply

Your email address will not be published. Required fields are marked *

Please put your code into a <pre>YOUR CODE</pre> section. Thanks and Happy Coding!