30 Kotlin Basic Exercises for Advanced with Solutions
Master advanced Kotlin skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in Kotlin, setting a solid foundation for professional-level challenges. Start your journey to Kotlin mastery today!
Learning Objectives:
Master advanced Kotlin features like coroutines, DSLs, generics, and extension functions for scalable development. Design robust, asynchronous, and reactive systems suited for enterprise and Android development.
Exercise Instructions:
- Start with the first exercise and attempt to solve it before checking the hint or solution.
- Ensure you understand the logic behind each solution, as this will help you in more complex problems.
- Use these exercises to reinforce your learning and identify areas that may require further study.
1. Write a function to find the intersection of three arrays.
Required Input:
Write a function to find the intersection of three arrays.
Expected Output:
Intersection: [3]
Code In Kotlin
Run Code?
Click Run Button to view compiled output
2. Implement a function to calculate the nth Fibonacci number iteratively.
Required Input:
A hardcoded integer, e.g., n = 7.
Expected Output:
The 7-th Fibonacci number is: 13
Code In Kotlin
Run Code?
Click Run Button to view compiled output
3. Write a function to check if a string is a valid palindrome ignoring spaces.
Required Input:
A hardcoded string, e.g., "A man a plan a canal Panama".
Expected Output:
The string is a palindrome
Code In Kotlin
Run Code?
Click Run Button to view compiled output
4. Create a program to rotate a matrix by 90 degrees clockwise.
Required Input:
A hardcoded matrix, e.g., [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]
Expected Output:
7 4 1
8 5 2
9 6 3
Code In Kotlin
Run Code?
Click Run Button to view compiled output
5. Write a function to count unique characters in a string.
Required Input:
A hardcoded string, e.g., "programming".
Expected Output:
Number of unique characters: 8
Code In Kotlin
Run Code?
Click Run Button to view compiled output
6. Implement a function to check if two strings are isomorphic.
Required Input:
Two hardcoded strings, e.g., "egg" and "add".
Expected Output:
The strings are isomorphic
Code In Kotlin
Run Code?
Click Run Button to view compiled output
7. Write a function to merge two sorted arrays into a single sorted array without duplicates.
Required Input:
Two hardcoded arrays, e.g., [1, 3, 5] and [2, 3, 6].
Expected Output:
Merged array: [1, 2, 3, 5, 6]
Code In Kotlin
Run Code?
Click Run Button to view compiled output
8. Create a program to calculate the greatest common divisor (GCD) of an array of integers.
Required Input:
A hardcoded array of integers, e.g., [12, 15, 18].
Expected Output:
GCD of the array: 3
Code In Kotlin
Run Code?
Click Run Button to view compiled output
9. Write a function to find the top k frequent elements in an array.
Required Input:
A hardcoded array of integers and an integer k, e.g., [1, 1, 1, 2, 2, 3] and k = 2.
Expected Output:
Top 2 frequent elements: [1, 2]
Code In Kotlin
Run Code?
Click Run Button to view compiled output
10. Write a function to find duplicate elements in an array.
Required Input:
A hardcoded array of integers, e.g., [1, 2, 3, 2, 4, 5, 3].
Expected Output:
Duplicate elements: [2, 3]
Code In Kotlin
Run Code?
Click Run Button to view compiled output