Skip to content

Commit 1f083de

Browse files
committed
LatestUniqueOrder, MaximumSubArray, TopKElements, Zalando tech screening
1 parent 412bcfe commit 1f083de

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main.kotlin.dsalgoleetcode
2+
3+
class LatestUniqueOrder {
4+
5+
fun latestUniqueOrders(orders: List<Order>): List<Order> {
6+
val latestMap = mutableMapOf<String, Order>()
7+
8+
for(order in orders){
9+
val current = latestMap[order.id]
10+
if(current == null || order.timestamp > current.timestamp){
11+
latestMap[order.id] = order
12+
}
13+
}
14+
return latestMap.values.toList()
15+
}
16+
}
17+
18+
data class Order(val id: String, val timestamp: Long)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main.kotlin.dsalgoleetcode
2+
3+
import kotlin.math.max
4+
5+
/**
6+
* Problem Statement:
7+
* Given an integer array nums, find the contiguous subarray (containing at least one number)
8+
* that has the largest sum, and return the sum.
9+
*
10+
* Input
11+
* val nums = intArrayOf(-2, 1, -3, 4, -1, 2, 1, -5, 4)
12+
*
13+
* Output
14+
* 6
15+
*
16+
* Explanation
17+
* The subarray [4, -1, 2, 1] has the largest sum = 6.
18+
* */
19+
20+
class MaximumSubArray {
21+
22+
fun maximumSubArray(input: IntArray): Int{
23+
24+
var currentSum = input[0]
25+
var maxSum = input[0]
26+
27+
for (element in input){
28+
currentSum = currentSum + element
29+
maxSum = max(maxSum, currentSum)
30+
currentSum = max(currentSum, 0)
31+
}
32+
return maxSum
33+
}
34+
}
35+
36+
fun main(){
37+
38+
val maximumSubArray = MaximumSubArray()
39+
println(maximumSubArray.maximumSubArray(intArrayOf(2,3,-8,7,-1, 2,3)))
40+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main.kotlin.dsalgoleetcode
2+
3+
/**
4+
* val nums = listOf("apple", "banana", "apple", "orange", "banana", "apple")
5+
* val k = 2
6+
* */
7+
8+
9+
class TopKElements {
10+
11+
fun topKFrequent(nums: List<String>, k: Int): List<String>{
12+
13+
return nums.groupingBy { it }
14+
.eachCount() //{"apple" -> 3, "banana" -> 2, "orange" -> 1}
15+
.entries // [Map.Entry("apple" → 3), Map.Entry("banana" → 2), ...]
16+
.sortedByDescending { it.value } // ["apple" (3), "banana" (2), "orange" (1)]
17+
.take(2)
18+
.map { it.key } // ["apple", "banana"]
19+
}
20+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main.kotlin.zalando
2+
3+
/**
4+
* Problem Statement:
5+
* You are given an array of integers, where each element represents a product value.
6+
* Return a new array result[] such that:
7+
*
8+
* result[i] is equal to the product of all elements in the original array except arr[i].
9+
*
10+
* [1, 2, 3, 4] => [24, 12, 8, 6]
11+
* */
12+
13+
class FetchProductExcludingCurrent {
14+
15+
fun fetchProduct(input: List<Int>): List<Int> {
16+
17+
var currentProduct = 1
18+
19+
val result = mutableListOf<Int>()
20+
21+
// for((element, index) in input.withIndex()){
22+
// var currentProduct = element * input[index + 1] *
23+
// }
24+
for (i in 0..input.size - 1) {
25+
for (j in 0..input.size - 1) {
26+
if (i == j) continue
27+
currentProduct = currentProduct * input[j]
28+
}
29+
result.add(currentProduct)
30+
currentProduct = 1
31+
}
32+
33+
println(result)
34+
return result
35+
}
36+
}

0 commit comments

Comments
 (0)