Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Kotlin
Kotlinhard

Flow vs Sequence — what’s the key difference?

Tags
#flow#sequence#streams
Back to categoryPractice quiz

Answer

`Sequence` is synchronous and lazy on the current thread. `Flow` is a cold asynchronous stream that can suspend, is cancellable, and is collected with coroutines (`collect`), which makes it good for async data streams.

flow {
  emit(1)
  emit(2)
}.collect { value ->
  println(value)
}

Advanced answer

Deep dive

`Sequence` is pull-based and synchronous: operations run when you iterate, on the current thread.

`Flow` is also cold (doesn’t run until collected), but it’s suspendable/cancellable and integrates with coroutines. It can represent async sources (DB, network, UI events) and supports operators with proper cancellation.

Practical guidance

  • Use `Sequence` for CPU-bound lazy transforms on in-memory data.
  • Use `Flow` for async streams and when you need cancellation/backpressure-ish behavior.

Common pitfalls

  • Expecting Flow to run on a background thread automatically (use `flowOn` / proper dispatcher).
  • Doing blocking IO inside Flow without switching context.

Related questions

Kotlin
Channel vs Flow - how are they different in coroutines?
#kotlin#coroutines#flow
Kotlin
StateFlow vs SharedFlow — what’s the practical difference?
#stateflow#sharedflow#flow
Java
Parallel streams: when can they help and what are common pitfalls?
#java#streams
#parallel
Java
Streams vs collections: what is the difference and a common pitfall?
#streams#collections#side-effects
PostgreSQL
Sequences/IDENTITY: why can generated IDs have gaps?
#sequence#identity#transactions