50 Scala Coding Exercises with Solutions

2/26/2026
All Articles

50 Scala Coding Exercises with Solutions

50 Scala Coding Exercises with Solutions

50 Scala Coding Exercises with Solutions

Scala is a powerful programming language that combines object-oriented and functional programming. Practicing coding problems is one of the best ways to master Scala syntax, collections, pattern matching, and functional concepts. Below are 50 Scala coding exercises with short solutions to help you strengthen your skills from beginner to advanced level.


Basic Level Exercises (1–15)

1. Print Hello World

println("Hello, World!")

2. Add Two Numbers

val a = 10
val b = 20
println(a + b)

3. Check Even or Odd

def isEven(n: Int) = n % 2 == 0

4. Swap Two Numbers

val a = 5
val b = 10
val swapped = (b, a)

5. Find Maximum of Two Numbers

def max(a: Int, b: Int) = if (a > b) a else b

6. Factorial of a Number

def factorial(n: Int): Int = if (n == 0) 1 else n * factorial(n - 1)

7. Fibonacci Series (First N Terms)

def fibonacci(n: Int): Int = if (n <= 1) n else fibonacci(n-1) + fibonacci(n-2)

8. Reverse a String

"Scala".reverse

9. Count Vowels in String

def countVowels(s: String) = s.count("aeiou".contains(_))

10. Check Palindrome

def isPalindrome(s: String) = s == s.reverse

11. Sum of List Elements

List(1,2,3).sum

12. Find Largest Element in List

List(4,7,1,9).max

13. Sort a List

List(3,1,4).sorted

14. Convert String to Uppercase

"scala".toUpperCase

15. Generate Random Number

scala.util.Random.nextInt(100)

🔹 Intermediate Level Exercises (16–35)

16. Remove Duplicates from List

List(1,2,2,3).distinct

17. Find Prime Number

def isPrime(n: Int) = (2 until n).forall(n % _ != 0)

18. Filter Even Numbers

List(1,2,3,4).filter(_ % 2 == 0)

19. Map Example (Square Numbers)

List(1,2,3).map(x => x * x)

20. Reduce Example (Sum)

List(1,2,3).reduce(_ + _)

21. Flatten Nested List

List(List(1,2), List(3,4)).flatten

22. Word Count in String

"Scala is powerful".split(" ").length

23. Find Second Largest Number

List(4,7,1,9).sorted.reverse(1)

24. Merge Two Lists

List(1,2) ++ List(3,4)

25. Convert List to Set

List(1,2,2,3).toSet

26. Count Frequency of Elements

List(1,2,2,3).groupBy(identity).mapValues(_.size)

27. Reverse a List

List(1,2,3).reverse

28. Check Anagram

def isAnagram(a: String, b: String) = a.sorted == b.sorted

29. Find GCD

def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)

30. Find LCM

def lcm(a: Int, b: Int) = (a * b) / gcd(a, b)

31. Take First N Elements

List(1,2,3,4).take(2)

32. Drop First N Elements

List(1,2,3,4).drop(2)

33. Check Armstrong Number

(Implement using digit power sum logic)

34. Find Length of List

List(1,2,3).length

35. Convert List to String

List(1,2,3).mkString(",")

🔹 Advanced Level Exercises (36–50)

36. Use Option Safely

val value: Option[Int] = Some(5)
value.getOrElse(0)

37. Pattern Matching Example

def matchTest(x: Any) = x match {
  case 1 => "one"
  case _ => "other"
}

38. Case Class Example

case class Person(name: String, age: Int)

39. Immutable vs Mutable Collections

(Practice using List vs ArrayBuffer)

40. Create Trait and Implement

trait Animal { def speak(): String }
class Dog extends Animal { def speak() = "Woof" }

41. Higher-Order Function

def applyFunc(f: Int => Int, x: Int) = f(x)

42. Use FoldLeft

List(1,2,3).foldLeft(0)(_ + _)

43. Implement Custom Map Function

(Use recursion over List)

44. Future Example

import scala.concurrent.Future

45. Try-Catch Example

try { 10/0 } catch { case e: Exception => println("Error") }

46. File Reading Example

(Use scala.io.Source.fromFile)

47. Implicit Parameter Example

implicit val multiplier = 2

48. Partial Function Example

val pf: PartialFunction[Int, String] = { case 1 => "one" }

49. Create Companion Object

class Demo
object Demo

50. Build Simple Calculator Function

def calculator(a: Int, b: Int, op: String) = op match {
  case "+" => a + b
  case "-" => a - b
  case "*" => a * b
  case "/" => a / b
}

Conclusion

Practicing these 50 Scala coding exercises with solutions will help you strengthen your understanding of Scala fundamentals, collections, recursion, functional programming, and advanced features. Regular practice combined with real-world project implementation will significantly improve your Scala programming confidence and problem-solving ability.

Article