10 Kotlin Code Challenges for Beginners

If you’re interested in mobile development, then you’ve probably already heard of Kotlin as a popular language for developing Android apps. Maybe you’ve even started an online Kotlin course to give yourself a head start in your new development career.

But learning doesn’t end once you’ve finished the course. If you want to keep your programming skills sharp, it’s important to constantly challenge yourself by solving problems using programming languages like Kotlin.

That’s why we’ve put together these 10 Kotlin code challenges for beginners to give you a way to put your knowledge to practical use and get you prepared for your next technical interview. Just pick a challenge below, hop in a workspace, and get started.

1. Number steps

Many programming languages have built-in functions that output sequences of numbers separated by steps. Create your own Kotlin function from scratch that creates a sequenced list of numbers from 0 to a maximum input value and input step. For example, a maximum value of 8 and a step value of 2 should return an output of [2, 4, 6, 8].

If you’re looking for more of a challenge, create a Fibonacci sequence generator. Remember that the following number of a Fibonacci sequence is the sum of the two preceding it: [1, 1, 2, 3, 5, 8, 13].

2. Consistent capitalization

An important part of data preparation is to ensure that data strings are uniformly formatted and capitalized. For a given input string, create a function that accepts a string input as well as an argument string that’s either “upper” or “lower.”

If “upper,” then the function should convert everything in the string to upper case, while the “lower” argument should return the string with every character in lowercase.

To give yourself an added challenge, add the arguments “snake” and “camel” that convert a string of words separated by spaces into snake case or camel case.

thisissnake_case

ThisIsCamelCase

3. The longest word

For a given input string of words, create a function that returns the longest word in the string. For example, for the input phrase “I love Codecademy,” the output should be “Codecademy.”

For an added challenge, you can create an output that lists the longest word along with the number of characters in the word. So, for the string “I love Codecademy,” the output would look something like, “word: Codecademy count: 10.”

4. Caesar cipher

In cryptography, a Caesar cipher is a simple encryption technique that shifts every letter in a message by a certain number of letters. The number of letters to shift is known as the key or shift parameter. For example, if the key is 1, then the message:

“Codecademy is awesome!”

becomes:

“Dpefdbefnz jt bxftpnf!”

Note how every letter in the encrypted message is one letter after each letter in the original message.

Create a Caesar cipher function that accepts an integer key and a message string as inputs. If the encrypted letter goes beyond the letter z, it should wrap around to the beginning of the alphabet.

For an extra challenge, create another function that decrypts an encoded message for a given key and encoded string.

5. Train route

Create a function that lists the stations on a train route based on an input of stations. For example, an input of (“NY Penn”) should output “Train is stopping at NY Penn.” For an input of (“NY Penn,” “Woodside,” “Forest Hills”), the output should be “Train is stopping at NY Penn, Woodside, and Forest Hills.”

If you want to challenge yourself a bit more, add a second input that includes train departure time, train number, and destination station. So, with the inputs (234, “10:00”, “Montauk”) and (“NY Penn,” “Babylon,” “Sayville”), the output should be “10:00 train number 234 to Montauk is stopping at NY Penn, Babylon and Sayville.”

Remember that the train number should be input as an integer but output as part of a string.

6. Permutation palindrome

A palindrome is a word or phrase that could be spelled the same forward and backward. Examples of palindromes include the words “level” and “racecar” as well as the phrase “taco cat.”

Create a function that returns TRUE if an input string is a permutation of a palindrome — that is, if the string could somehow be rearranged to make a palindrome. For example, the strings “racecar,” “carrace,” and “gghhk” should return TRUE, while words such as “tammy” and “code” should return FALSE.

For an additional challenge, allow your input to include phrase strings. You’ll need to find a way to remove all spaces in the input string before running your function.

7. FizzBuzz

The FizzBuzz challenge is a popular problem in technical interviews. For a given maximum input value n, create a function that outputs integers from 1 to n.

But, if an integer is divisible by three, then the number should be replaced with the word “Fizz.” Numbers divisible by five should say “Buzz” instead. And numbers divisible by both three and five should say “FizzBuzz.”

For example, with an input of 17, the output should be:

[1, 2 , ”Fizz”, 4, “Buzz”, “Fizz”, 7, 8, “Fizz“, “Buzz“, 11, “Fizz”, 13, 14, “FizzBuzz”, 16, 17]

8. Steps

For a given integer input n, create a function that returns a string output of generated steps using the # character. For example, for an input of 3, the output should look like:

‘# ‘

‘##’

‘###’

If you’d like to “step up” this challenge, add a string input that allows the function to generate steps for any given single character.

9. Triangles, etc.

Come up with a function that creates a triangle based on an integer input to specify the number of rows and a string input to determine the “building block” of the triangle. If you want to challenge yourself more, add a third input that allows someone to specify different shapes such as a circle, square, or trapezoid.

10. Pascal’s triangle

If you’re up to an intermediate challenge after finishing Challenge 9 (Triangles, etc.), try creating Pascal’s triangle.

You might remember learning about Pascal’s triangle in algebra class. It’s a quick way to solve binomial expansions — (x + y)n — where the nth row in Pascal’s triangle gives you the coefficients of xy raised to different powers. Pascal’s triangle is also used in probability theory, and you can read more about how it’s created here.

For a given integer input a, create Pascal’s triangle with a rows. The first row should correspond to a = 0.

For example, for an input value of 2, the output should look something like:

1

1,1

1,2,1

If you’re pursuing a career in mobile development, learning Kotlin is a great way to get started. Or, if you want to learn more about Android development, check out courses like:

Learn the Basics of AndroidBuild Basic Android Apps With Java

Note that Kotlin also has uses in web development, so it’s also a great tool for anyone looking to become a Front-End, Back-End, or Full-Stack Engineer. To learn more, take a look through our article on what Kotlin is used for.

Mobile Development Courses & Tutorials | Codecademy
Every year more and more people rely on mobile devices to meet their needs. Where websites used to be the gold-standard, people now rely on mobile apps. The technologies used to create these apps are expanding and improving quickly, so it’s an exciting time to start learning Mobile Development!
Codecademy

Read More

Leave comment

Total
0
Share