Skip to content

Instantly share code, notes, and snippets.

@vengateshm
Last active November 8, 2023 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vengateshm/b23e76aa76a1e4273e4066800cea9c45 to your computer and use it in GitHub Desktop.
Save vengateshm/b23e76aa76a1e4273e4066800cea9c45 to your computer and use it in GitHub Desktop.
Android kotlin interview questions and answers

1. What is a memory leak?

When a computer program or application does not free up the memory which they allocated for performing an operation then memory leak occurs. Inner classes created inside activity hold implicit reference to activity object. If activity destroyed and inner class still in memory then it prevents activity object to be freed from memory causing a leak.

2. What is ANR in Android?

When UI thread is occupied for long time then Application Not Responding dialog pops up. Several reasons like network or database operation on UI thread, performing computationally intensive tasks in UI thread like complex mathematical calculation, bitmap manipulation can lead to ANR.

3. What is coroutine?

It can be considered as a light weight thread that can be paused and resumed without blocking the thread. Kotlin coroutines helps to write asynchronous code in sequential manner. If you make a network call inside a suspend function it is paused until it gets response from server. When it is in paused state that thread is free to perform other tasks.

4. Difference between coroutine and flow

For single suspending computation. Launch, async are coroutine builders. Flows used to handle stream of asynchronous values. Map, filter, transform operation can be done on flows. It supports back pressure handling.

5. What are the Kotlin delegates?

In general delegation is a design pattern in which responsibility of a task is delegated to another object. This helps code reusability. In kotlin by keyword used for delegation. Built in delegates in kotlin are lazy, observable, vetoable.

6. How to create a singleton in Kotlin?

Create using object keyword, create a class with companion object and inside that create instance and expose it via public method.

7. What is MediatorLiveData for?

It is a part of Android architecture component. Used to combine 2 or more live data sources. It can be used to perform transformations like map, filter etc. Instead of observing individual live data sources we can observe mediator live data object.

8. Difference between Zip, Merge and Combine when using flows.

Zip operator combines latest values emitted by multiple flows. It waits for all flows to emit a value. 

Combine operator also combines latest values emitted but it emits as soon as any of the input flow emits a value. 

Merge operator emits values whenever any of the input flow emits a value.

9. What is Context in Android?

It is the base type in android like Object in Java. It represents the current state of the application and provide info about applications environment. It used to access application data like resources, start other components like service, activity, register for broadcast, send broadcast etc.

10. Difference between GlobalScope and CoroutineScope.

GlobalScope – Scoped to lifecycle of a application which means they execute until they are cancelled explicitly or the application finishes. It is used for task that takes less time.

CoroutineScope – To create custom scope and we can manage the coroutines created in this scope we can cancel when the coroutines are not needed.

11. If we make 2 network calls, each using its own coroutine. How can we make them execute in parallel? (2nd coroutine shouldn’t wait for the 1st one to complete)

We can use launch or async coroutine builder and make two network calls in two builder functions. We can use 2 different scopes two run the 2 coroutines for better lifecycle handling of two coroutines.

12. Difference between cold flow and hot flow.

Cold flow – starts emitting when terminal operator is called or when a collector is present. Every time new instance of flow created when we collect. 

Hot flow – emits data regardless of whether any collectors attached or not. When we start collecting a hot flow it receives new values which are emitted and old values are lost. Some values can be replayed based on the api level implementation. State flow and shared flow are hot flows in kotlin coroutines.

13. Difference between explicit and implicit intent

Target Android component class or package name is mentioned while creating intent object. Eg specifying activity name while starting the activity. Implicit intent – target component class not mentioned explicitly rather action is set while creating intent object. The OS implicitly tries to resolve the components that handles the specified action. ACTION_SEND with type text/plain can open a share dialog with all apps that can handle text data.

14. How does Lazy instantiation work?

Object is initialised when it is accessed for the first time. Further access will not create new object rather it will return the object created earlier.In Kotlin we use lazy keyword.

15. What is a Dispatcher?

It determines where the coroutine can run (thread in which coroutine can run). Dispatcher is a part of CoroutineContext.

16. How many Dispatchers are there?

Kotlin has some built in dispatchers like Default, IO, Main, Unconfined, Main.immediate, Unspecified. In addition to this we can extend CoroutineDispatcher to create custom dispatchers.

Coding programs :

  1. Write a function in Kotlin that takes in a string as input and returns the most frequently occurring character in the string. If multiple characters have the same highest frequency, return any one of them.

  2. Write a function in Kotlin that checks if a given string is a palindrome (reads the same forwards and backwards), ignoring whitespace and punctuation.

  3. Implement a function in Kotlin that finds the second smallest and second largest elements in an array of integers.

  4. Given two unsorted arrays A of size N and B of size M of distinct elements, the task is to find all pairs from both arrays whose sum is equal to X.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment