# Unofficial Solutions to the Training by Codility

codility.com 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 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

### 41 Replies to “Unofficial Solutions to the Training by Codility”

1. Sheng says:

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!

• Guillermo says:

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,

Regards

Guillermo

• Sheng says:

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!

• Sheng says:

Thanks for sharing! I am crazy busy these days. I saw the new training questions, but did not have time to solve them~

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 ðŸ™‚

Thanks!!

• ps: take a look at learn-c.org and how we can all contribute to lessons using Github only ðŸ™‚

• Sheng says:

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?

• Sheng says:

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. Julian says:

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. Julian says:

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.

• Sheng says:

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.

• Lars says:

You say that “Codility is the best online judge system with free training” you have ever used.

Can you share what other services you tried, and which your would recommend ?

5. Winger says:

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.

• Sheng says:

I will. But possibly not now. Codility is hard. It costs me days to solve it. Currently, I do not have so much time, because of my job hunting.

• Winger says:

Good luck! Iâ€˜ll focus on codility, and wait for your good solution.Maybe we can discuss the latest challenge, together.

• Sheng says:

Thanks! And enjoy coding!

6. Pa says:

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.

Pa

• Sheng says:

My pleasure! And Enjoy coding!

7. Zheng says:

I have a golden solution for Chlorum 2014, which is the newest challenge.

• Sheng says:

That is awesome! But please do not share it while it is on duty ðŸ™‚

• I’ll explain my solution on my blog once Chlorum 2014 is over.

8. SA says:

Codility Dynamic programming lessons are missing from this collection. Check here
https://codility.com/programmers/lessons/16
I am posting the solution of NumberSolitaire. https://codility.com/demo/take-sample-test/number_solitaire

• alexey says:

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

• Sheng says:

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.

9. Anonymous Says says:

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 .

• Sheng says:

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.

10. Sebastian Cheung says:

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

• Sheng says:

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

• I don’t know about Python, but heap is included in the standard Java library. Java has the “Priority Queue,” which is an implementation of heap.

• Sheng says:

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

• Is “heapq” what you were referring to?
https://docs.python.org/2/library/heapq.html

Heapq is a Min heap by default. Does Python have a Max heap that stores objects?

In Java, we could supply a custom Compare method to Java’s implementation of heap to create Max heap and Min heap. I would like know how to do that in Python.

• Sheng says:

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.

11. Sebastian Cheung says:

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?

• Sheng says:

Exceptions are not necessary. At lease, my solutions did not use any exception.

12. Sebastian Cheung says:

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?

• Sheng says:

You might be correct. Few times, I used “import” inside a function, but it is really ugly.

• Sheng says:

Sorry, I am busy these days. I did not have time to work on the new challenges.

13. Amazing works. Really appreciated. Thanks.