Skip to content

Instantly share code, notes, and snippets.

@aroranubhav
aroranubhav / LruCache.kt
Created February 20, 2025 06:24
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache
class LRUCache(private val capacity: Int) {
inner class Node(
val key: Int,
val value: Int,
var next: Node? = null,
var prev: Node? = null
)
private val cache = mutableMapOf<Int, Node>()
@aroranubhav
aroranubhav / ClassInstanceViaReflection.kt
Created March 3, 2025 20:23
Creating class instance via Reflection Example
class SomeClass {
fun doTask() {
for (i in 1..10) {
println(i)
}
}
}
fun <T> createClassInstance(someClass: Class<T>): T{
return someClass.getDeclaredConstructor().newInstance()
@aroranubhav
aroranubhav / ClassInstanceViaFactory.kt
Created March 3, 2025 20:35
Classs Instance Via Factory
fun interface Factory<T> {
fun create(): T
}
fun <T> createInstance(factory: Factory<T>): T {
return factory.create()
}
fun main() {
val instance = createInstance(Factory {
@aroranubhav
aroranubhav / ExampleComponent.kt
Created March 5, 2025 08:20
Dagger DI in Fragments
@FragmentScope
@Component(
dependencies = [ApplicationComponent::class],
modules = [ExampleModule::class]
)
interface ExampleComponent {
fun inject(fragment: ExampleFragment)
}