Kotlin Interview Preparation Tutorial: Key Topics, val vs var, lateinit vs lazy, Coroutines, Scope Functions, Data Classes, and Sealed Classes


This Kotlin Interview Preparation tutorial covers essential interview topics including val vs var, lateinit vs lazy, coroutines vs threads, data class usage, scope function differences, and sealed classes vs enums. The chapter provides concise examples, best practices, and insights to help developers prepare for Kotlin interviews and advance their careers.

Kotlin Interview Topics (Complete Guide)

val vs var

  1. val: Immutable reference (cannot be reassigned).
  2. var: Mutable reference (can be reassigned).

Example


val name = "Muni" // Cannot reassign
var age = 35 // Can reassign
age = 36

Best Practice

  1. Prefer val unless mutability is required
  2. Improves thread safety and readability

lateinit vs lazy

lateinit

  1. Used for variables initialized later
  2. Must be non-nullable
  3. Only works with var

lateinit var user: User
user = User("Muni")

lazy

  1. Initialized on first access
  2. Works with val
  3. Thread-safe by default

val user: User by lazy { User("Muni") }

Best Practice

  1. Use lazy for expensive initialization
  2. Use lateinit for dependency injection or Android Views

Coroutines vs Threads

FeatureThreadsCoroutines
LightweightHeavyVery lightweight
Creation costExpensiveCheap
BlockingYesNo (suspend functions)
Use caseLow concurrency tasksHigh concurrency, I/O tasks

Example Coroutine


GlobalScope.launch {
delay(1000L)
println("Running asynchronously")
}

Best Practice

  1. Prefer coroutines for async tasks
  2. Avoid blocking main thread in Android
  3. Use structured concurrency (CoroutineScope)

Data Class Usage

  1. Automatically provides equals(), hashCode(), toString(), and copy().
  2. Ideal for immutable data holders.

Example


data class User(val name: String, val age: Int)
val user1 = User("Muni", 35)
val user2 = user1.copy(age = 36)

Best Practice

  1. Use for DTOs, API responses, and immutable objects

Scope Function Differences

Kotlin has five scope functions: let, run, with, apply, also.

FunctionContext ObjectReturnUse Case
letitLambda resultNull-safety, chaining
runthisLambda resultObject configuration
withthisLambda resultObject configuration, no extension
applythisObjectObject initialization
alsoitObjectSide-effects, logging

Example


val result = "Kotlin".let { it.uppercase() } // Returns "KOTLIN"

Best Practice

  1. Choose scope function based on this/it and return requirement
  2. Use apply for builder-style initialization

Sealed Class vs Enum

Enum

  1. Represents fixed set of constants
  2. Simple use cases

enum class Status { SUCCESS, ERROR, LOADING }

Sealed Class

  1. Represents restricted class hierarchy
  2. Can contain state and data
  3. Useful for complex state management

sealed class Result {
data class Success(val data: String) : Result()
data class Error(val error: Throwable) : Result()
}

Best Practice

  1. Use enums for fixed constants
  2. Use sealed classes for result/state modeling

Summary

This chapter covered essential Kotlin interview topics: val vs var, lateinit vs lazy, coroutines vs threads, data class usage, scope function differences, and sealed classes vs enums. Understanding these topics, along with practical examples, prepares developers for Kotlin interviews and career growth.