30 Kotlin Basic Exercises for Intermediate with Solutions
Master intermediate 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 advanced challenges. Start your journey to Kotlin mastery today!
Learning Objectives:
Deepen your knowledge of object-oriented programming, collections, null safety, and higher-order functions in Kotlin. Build more structured applications using classes, interfaces, and Kotlin’s standard library features.
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 Kotlin program to find the second largest element in an array.
Required Input:
A hardcoded array: [10, 20, 30, 40, 50]
Expected Output:
The second largest element is: 40
Code In Kotlin
Run Code?
Click Run Button to view compiled output
2. Create a program to check if a string is a valid palindrome, ignoring case and non-alphanumeric characters.
Required Input:
A hardcoded string: "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
3. Write a function to rotate an array k times to the right.
Required Input:
A hardcoded array [1, 2, 3, 4, 5] and k = 2
Expected Output:
Rotated array: 4, 5, 1, 2, 3
Code In Kotlin
Run Code?
Click Run Button to view compiled output
4. Implement a program to find the first non-repeating character in a string.
Required Input:
A hardcoded string: "swiss"
Expected Output:
The first non-repeating character is: w
Code In Kotlin
Run Code?
Click Run Button to view compiled output
5. Write a function to calculate the power of a number using recursion.
Required Input:
A hardcoded base 2 and exponent 3
Expected Output:
2 raised to the power of 3 is: 8
Code In Kotlin
Run Code?
Click Run Button to view compiled output
6. Create a program to sort an array of integers in descending order.
Required Input:
A hardcoded array: [5, 2, 8, 1, 3]
Expected Output:
Sorted array in descending order: 8, 5, 3, 2, 1
Code In Kotlin
Run Code?
Click Run Button to view compiled output
7. Write a Kotlin program to merge two sorted arrays into one sorted array.
Required Input:
Hardcoded arrays: [1, 3, 5] and [2, 4, 6]
Expected Output:
Merged and sorted array: 1, 2, 3, 4, 5, 6
Code In Kotlin
Run Code?
Click Run Button to view compiled output
8. Implement a program to calculate the sum of all digits in a number until a single digit is obtained (digital root).
Required Input:
A hardcoded number: 493193
Expected Output:
The digital root is: 2
Code In Kotlin
Run Code?
Click Run Button to view compiled output
9. Write a function to check if two strings are rotations of each other.
Required Input:
Hardcoded strings: "abcd" and "cdab"
Expected Output:
cdab is a rotation of abcd
Code In Kotlin
Run Code?
Click Run Button to view compiled output
10. Create a program to count the frequency of words in a given string.
Required Input:
A hardcoded string: "this is a test this is only a test"
Expected Output:
Word frequencies: {this=2, is=2, a=2, test=2, only=1}
Code In Kotlin
Run Code?
Click Run Button to view compiled output