Skip to content

Instantly share code, notes, and snippets.

@padymont
padymont / polling.kts
Last active January 30, 2026 15:50
Polling 30012026
// Task:
// Implement polling with a timeout
suspend fun makeNetworkRequest(): Result<String> {
delay(100)
return if (Random.nextBoolean()) {
Result.success("hello")
} else {
Result.failure(RuntimeException("Bad luck"))
}
@padymont
padymont / kotlin-coroutines-test.kts
Created January 27, 2026 09:00
Kotlin Coroutines Test
// What it the output?
import kotlinx.coroutines.*
import kotlinx.coroutines.test.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
@OptIn(ExperimentalCoroutinesApi::class)
fun main() = runTest {
launch(StandardTestDispatcher(testScheduler)) {
println("Standard runs")
@padymont
padymont / kotlin-coroutines-240220260936.kts
Last active January 26, 2026 13:12
Kotlin Coroutines 240220260936
// 1.
// Какой будет вывод?
// Как сделать так, чтобы корутины выполнялись асинхронно и параллельно?
fun main() = runBlocking {
launch {
Thread.sleep(3000)
println(1)
}
launch {
@padymont
padymont / coroutines-23012026.kts
Last active January 23, 2026 14:18
Coroutines 230120261438
//
// Task 1
// What is the output?
//
import kotlinx.coroutines.*
fun main() {
runBlocking {
val request = launch {
@padymont
padymont / init-order.kts
Created January 23, 2026 13:20
Init Order
// What is the output of the function?
fun main() {
Habr()
}
class Habr {
init {
searchMagnit()
//
// launch + try-catch
//
viewModelScope.launch {
XXXXXXXXX {
launch {
try {
throw RuntimeException("Bad luck")
} catch (e: Exception) {
@padymont
padymont / ChatSessionController.kts
Last active January 20, 2026 09:31
Live coding challenge 19012026
// Task: fix and refactor
object Const {
val INDEX = 1
}
private var TAG = "ChatSessionController"
open class ChatSessionController(
private val accountRepository: ChatAccountRepository,
@padymont
padymont / kotlin_coroutines_retrying_network_calls.kts
Last active January 12, 2026 18:08
Kotlin Coroutines Retrying Network Calls
// You are fetching a user's Account Balance.
// The network is flaky and frequently throws IOException.
// You need to implement a robust retry mechanism.
//
// Create a utility function called retryWithBackoff and use it to call fetchBalance().
//
// If the call fails, retry up to 3 times.
//
// The delay between retries should double each time.
// 1st failure: Wait 1000ms
@padymont
padymont / kotlin_coroutines_exceptions_handling.kts
Created January 12, 2026 14:48
Kotlin Coroutines Exceptions Handling Task 154612012026
// Task
//
// Implement a function fetchTripSummary() using Kotlin Coroutines
// that meets the following requirements:
//
// - Concurrent Execution: All three services must be called simultaneously to minimize loading time.
//
// - Selective Resilience: * If fetchWeather() throws an exception, the function should continue
// and return the summary with a null weather field.
// If fetchFlight() or fetchHotel() throws an exception, all other active fetches should be
@padymont
padymont / generic_functions.kts
Last active December 9, 2025 13:22
Generic functions // Kotlin Interview Question
// Task: will this code compile?
fun <T> filterList(list: List<Any>, action: () -> Unit): List<T> {
val resultList = mutableListOf<T>()
for (element in list) {
if (element is T) {
resultList.add(element)
} else {
saveResult(action)
}