Tasks Details
easy
Find and correct bugs in a function that checks whether A contains numbers 1, 2, ..., K and no other numbers.
Task Score
0%
Correctness
0%
Performance
Not assessed

You are given an implementation of a function:

def solution(A, K)

This function, given a non-empty array A of N integers (sorted in non-decreasing order) and integer K, checks whether A contains numbers 1, 2, ..., K (every number from 1 to K at least once) and no other numbers.

For example, given the following array A, and K = 3:

A[0] = 1 A[1] = 1 A[2] = 2 A[3] = 3 A[4] = 3

The function should return True.

For the following array A, and K = 2:

A[0] = 1 A[1] = 1 A[2] = 3

the function should return False.

The attached code is still incorrect for some inputs. Despite the error(s), the code may produce a correct answer for the example test cases. The goal of the exercise is to find and fix the bug(s) in the implementation. You can modify at most two lines.

Assume that:

  • N and K are integers within the range [1..300,000];
  • each element of array A is an integer within the range [0..1,000,000,000];
  • array A is sorted in non-decreasing order.

In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Python
Time spent on task 5 minutes
easy
Create a simple like button component with React.
Task Score
100%
Correctness
100%
Performance
Not assessed

Build a "like button" component using React 16. The component should be the default export (use export default).

Requirements:

1. There should be a like button:

  • The content of the like button should be in the following format: "Like | 100", where 100 is the total number of likes.
  • It should have a "like-button" class.
  • Wrap the number of likes in a span with a "likes-counter" class.
  • The initial number of likes in the counter should be 100.

2. Users can add a like. By clicking the button:

  • The number of likes should increase by one.
  • Like button should have "liked" class in addition to the "like-button" class (you can use the classnames tool for that).

3. Users can undo their like by clicking again on the button:

  • The counter should decrease by one.
  • "liked" class should be removed.

Assessment/Tools:

  • Only two imports are allowed: react (v16.8.6) and classnames (v2.2.5). Both are at the top of the starting code.
  • Use the animation below as a reference for your solution.
  • Design/styling is not assessed and will not affect the score. You should focus only on implementing the requirements.
  • The "Preview" tab will display your component. You can use it for testing purposes.


Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used JavaScript
Time spent on task 2 minutes
medium
Count the minimum number of letters that must be deleted from a word to create a word in which no two letters occur the same number of times.
Task Score
100%
Correctness
100%
Performance
100%

Write a function:

def solution(S)

that, given a string S consisting of N lowercase letters, returns the minimum number of letters that must be deleted to obtain a word in which every letter occurs a unique number of times. We only care about occurrences of letters that appear at least once in result.

Examples:

1. Given S = "aaaabbbb", the function should return 1. We can delete one occurrence of a or one occurrence of b. Then one letter will occur four times and the other one three times.

2. Given S = "ccaaffddecee", the function should return 6. For example, we can delete all occurrences of e and f and one occurrence of d to obtain the word "ccaadc". Note that both e and f will occur zero times in the new word, but that is fine, since we only care about letters that appear at least once.

3. Given S = "eeee", the function should return 0 (there is no need to delete any characters).

4. Given S = "example", the function should return 4.

Write an efficient algorithm for the following assumptions:

  • N is an integer within the range [0..300,000];
  • string S is made only of lowercase letters (az).
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Python
Time spent on task 6 minutes