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
- Binary-Gap ★: Python solution
Lesson 2: Arrays
- Odd-Occurrences-In-Array ★: Python solution
- Cyclic-Rotation ★: Python/C++ solution
Lesson 3: Time Complexity
- Frog-Jmp ★: Python solution
- Perm-Missing-Elem ★: Python solution
- Tape-Equilibrium ★: Python solution
Lesson 4: Counting Elements
- Perm-Check ★: Python solution Java solution
- Frog-River-One ★: Python solution
- Max-Counters ★★: Python solution
- Missing-Integer ★★: Python solution
Lesson 5: Prefix Sums
- Passing-Cars ★: Python solution
- Genomic-Range-Query ★★: Python solution
- Min-Avg-Two-Slice ★★: Python solution
- Count-Div ★★: Python solution
Lesson 6: Sorting
- Distinct ★: Python solution C solution
- Max-Product-Of-Three ★: Python solution
- Triangle ★★: Python solution
- beta2010 (Number-Of-Disc-Intersections) ★★★: Python solution
Lesson 7: Stacks and Queues
- Brackets ★: Python solution
- Nesting ★: Python solution
- sigma2012 (Stone-Wall) ★★: Python solution
- Fish ★★: Python solution
Lesson 8: Leader
- Dominator ★: Python solution
- Equi-Leader ★: Python solution
Lesson 9: Maximum Slice Problem
- Max-Double-Slice-Sum ★★: Python solution
- Max-Profit ★★: Python solution
- Max-Slice-Sum ★★: Python solution
Lesson 10: Prime and Composite Numbers
- Min-Perimeter-Rectangle ★: Python solution
- Count-Factors ★: Python solution
- Peaks ★★: Python solution
- boron2013 (Flags) ★★★: Python solution
Lesson 11: Sieve of Eratosthenes
- Count-Non-Divisible ★★: Python solution
- Count-Semiprimes ★★: Python solution
Lesson 12: Euclidean Algorithm
- Chocolates-By-Numbers ★: Python solution
- Common-Prime-Divisors ★★: Python solution
Lesson 13: Fibonacci Numbers
- Fib-Frog ★★: Python solution
- Ladder ★★: Python solution
Lesson 14: Binary Search Algorithm
- Min-Max-Division ★★: Python solution C++ solution
- Nailing-Planks ★★: Python solution C++ solution
Lesson 15: Caterpillar Method
- Count-Triangles ★: Python solution
- Abs-Distinct ★: Python solution
- Count-Distinct-Slices ★★: Python solution
- Min-Abs-Sum-Of-Two ★★★: Python solution
Lesson 16: Greedy Algorithms
- Tie-Ropes ★: Python solution
- Max-Nonoverlapping-Segments ★: Python solution
Lesson 17: Dynamic Programming
- Number-Solitaire ★★: Python solution
- Min-Abs-Sum ★★★: Python solution
Lesson 90: Tasks from Indeed Prime 2015 challenge
- Longest-Password ★: Python solution
- Flood-Depth ★★: Python solution
- Slalom-Skiing ★★★: Python solution
Lesson 91: Tasks from Indeed Prime 2016 challenge
- Dwarfs-Rafting ★: Python solution
Lesson 99: Future Training
- Tree-Height ★: Python solution
- Str-Symmetry-Point ★: Python solution
- Equi ★: Python solution
- Sql-Sum ★: Python solution
- Array-Inversion-Count ★★: Python solution
- Polygon-Concavity-Index ★★★: Python solution
- alpha2010 (Prefix-Set): Python solution
- beta2010 (Number-Of-Disc-Intersections): Python solution
- gamma2011 (Count-Palindromic-Slices): Python solution
- Delta2011 (Min-Abs-Sum): Python solution
- nu2011 (Double-Median): Python solution
- sigma2012 (Stone-Wall): Python solution
- upsilon2012 (Cartesian-Sequence): Python solution
- chi2012 (Cannonballs): Python solution
- psi2012 (Wire-Burnouts): Python solution
- omega2013 (Falling-Disks): Python solution
- helium2013 (Find-Three): Python solution
- lithium2013 (Clocks): Python solution
- boron2013 (Flags): Python solution
- oxygenium2014 (Count-Bounded-Slices): Python solution
Interview Questions (Following the request from Codility, all the questions & solutions are removed.):
- Count Arithmetic Sequence:
- Tree Amplitude:
- Best Shuffle:
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 “<”. For greater than >, use “>”
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!
Thanks for posting solutions!! They really helped.
I saw at least one missing, so posting here, so that others may benefit.
Thanks for sharing! I am crazy busy these days. I saw the new training questions, but did not have time to solve them~
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 🙂
ps: take a look at learn-c.org and how we can all contribute to lessons using Github 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.
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…
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.
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 ?
Other than Codility, I would like to recommend LeetCode (https://oj.leetcode.com/).
Used Jobdu (http://ac.jobdu.com/hhtproblems.php) for a while, not recommended.
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.
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.
Good luck! I‘ll focus on codility, and wait for your good solution.Maybe we can discuss the latest challenge, together.
Thanks! And enjoy coding!
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.
My pleasure! And Enjoy coding!
I have a golden solution for Chlorum 2014, which is the newest challenge.
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.
Codility Dynamic programming lessons are missing from this collection. Check here
I am posting the solution of NumberSolitaire. https://codility.com/demo/take-sample-test/number_solitaire
[-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.
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.
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.
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.
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?
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.
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.
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?
Exceptions are not necessary. At lease, my solutions did not use any exception.
But I think somewhere I saw in Codility, you can import inside a function?
from math import random
but not on top level import?
You might be correct. Few times, I used “import” inside a function, but it is really ugly.
Hi! Have you got some pointers for https://codility.com/programmers/lessons/92-tasks_from_indeed_prime_2016_college_coders_challenge/array_recovery/? Thanks,
Sorry, I am busy these days. I did not have time to work on the new challenges.
I also cannot manage to solve this question.
It seems they changed the link:
Have you achieved a suitable solution? Thanks.
Amazing works. Really appreciated. Thanks.
My solution pythonic of MinAvgTwoSlice in O(n) got 100%:
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:
Hi Sheng! Thanks for the very elegant answers.
This is my answer of ThreeLetters from the “Coding Skills” section:
My C++ solution of CountConformingBitmasks from the Bitwise operations (bit-ops) section:
My C++ solution for SparseBinaryDecomposition from the Bitwise Operations section:
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.
This is my C++ solution for the CountriesCount (Data Structures section):
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:
I’m working for days to solve ArrayRecovery:
So far, I tried using prunning BFS with no luck due to the severe time complexity achieved.
Any suggestions are very welcome!