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

fun main() { val numbers = arrayOf(10, 20, 30, 40, 50) // Add your code here to find the second largest element }

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

fun main() { val input = "A man, a plan, a canal: Panama" // Add your code here to check if the string is a valid palindrome }

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

fun rotateArray(arr: Array<Int>, k: Int): Array<Int> { // Add your code here to rotate the array } fun main() { val array = arrayOf(1, 2, 3, 4, 5) val k = 2 println("Rotated array: ${rotateArray(array, k).joinToString(", ")}") }

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

fun main() { val input = "swiss" // Add your code here to find the first non-repeating character }

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

fun power(base: Int, exponent: Int): Int { // Add your recursive code here } fun main() { val base = 2 val exponent = 3 println("$base raised to the power of $exponent is: ${power(base, exponent)}") }

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

fun main() { val numbers = arrayOf(5, 2, 8, 1, 3) // Add your code here to sort the array in descending order }

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

fun mergeSortedArrays(arr1: Array<Int>, arr2: Array<Int>): Array<Int> { // Add your code here to merge and sort the arrays } fun main() { val arr1 = arrayOf(1, 3, 5) val arr2 = arrayOf(2, 4, 6) println("Merged and sorted array: ${mergeSortedArrays(arr1, arr2).joinToString(", ")}") }

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

fun digitalRoot(num: Int): Int { return if (num < 10) num else digitalRoot(num.toString().map { it - '0' }.sum()) } fun main() { val number = 493193 println("The digital root is: ${digitalRoot(number)}") }

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

fun areRotations(str1: String, str2: String): Boolean { // Add your code here to check if the strings are rotations } fun main() { val str1 = "abcd" val str2 = "cdab" if (areRotations(str1, str2)) { println("$str2 is a rotation of $str1") } else { println("$str2 is not a rotation of $str1") } }

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

fun main() { val text = "this is a test this is only a test" // Add your code here to count word frequencies }

Run Code?

Click Run Button to view compiled output

ad vertical

1 of 3