21. Write a function to generate the nth row of Pascal’s Triangle.
Required Input:
A hardcoded value, e.g., n = 5.
Expected Output:
Pascal's Triangle row 5: [1, 4, 6, 4, 1]
Code In Kotlin
fun pascalRow(n: Int): List<Int> {
// Add your code here to generate the nth row
}
fun main() {
val n = 5
println("Pascal's Triangle row $n: ${pascalRow(n)}")
}
Run Code?
Click Run Button to view compiled output
22. Implement a function to check if a given string contains balanced parentheses.
Required Input:
A hardcoded string, e.g., "(a + b) * (c - d)".
Expected Output:
The string has balanced parentheses
Code In Kotlin
fun isBalanced(input: String): Boolean {
// Add your code here to check for balanced parentheses
}
fun main() {
val input = "(a + b) * (c - d)"
if (isBalanced(input)) {
println("The string has balanced parentheses")
} else {
println("The string does not have balanced parentheses")
}
}
Run Code?
Click Run Button to view compiled output
23. Write a function to find the maximum sum of a contiguous subarray (Kadane's Algorithm).
Required Input:
A hardcoded array, e.g., [-2, 1, -3, 4, -1, 2, 1, -5, 4].
Expected Output:
Maximum sum of contiguous subarray: 6
Code In Kotlin
fun maxSubArray(nums: Array<Int>): Int {
// Add your code here to find the maximum subarray sum
}
fun main() {
val nums = arrayOf(-2, 1, -3, 4, -1, 2, 1, -5, 4)
println("Maximum sum of contiguous subarray: ${maxSubArray(nums)}")
}
Run Code?
Click Run Button to view compiled output
24. Create a program to sort an array of 0s, 1s, and 2s in-place.
Required Input:
A hardcoded array, e.g., [2, 0, 2, 1, 1, 0].
Expected Output:
Sorted array: [0, 0, 1, 1, 2, 2]
Code In Kotlin
fun sortColors(nums: Array<Int>): Array<Int> {
// Add your code here to sort the array in-place
}
fun main() {
val nums = arrayOf(2, 0, 2, 1, 1, 0)
println("Sorted array: ${sortColors(nums).joinToString(", ")}")
}
Run Code?
Click Run Button to view compiled output
25. Write a function to find the square root of a number using the binary search approach.
Required Input:
A hardcoded number, e.g., 25.
Expected Output:
Square root of 25 is: 5
Code In Kotlin
fun sqrtBinarySearch(x: Int): Int {
// Add your code here to find the square root using binary search
}
fun main() {
val x = 25
println("Square root of $x is: ${sqrtBinarySearch(x)}")
}
Run Code?
Click Run Button to view compiled output
26. Implement a function to reverse the digits of an integer without converting it to a string.
Required Input:
A hardcoded integer, e.g., 12345.
Expected Output:
Reversed integer: 54321
Code In Kotlin
fun reverseInteger(num: Int): Int {
// Add your code here to reverse the digits
}
fun main() {
val num = 12345
println("Reversed integer: ${reverseInteger(num)}")
}
Run Code?
Click Run Button to view compiled output
27. Write a function to find the intersection of two linked lists.
Required Input:
Two linked lists:
List A: 1 -> 2 -> 3 -> 4 -> 5
List B: 9 -> 8 -> 3 -> 4 -> 5
Expected Output:
Intersection starts at node with value: 3
Code In Kotlin
class ListNode(val value: Int) {
var next: ListNode? = null
}
fun findIntersection(headA: ListNode?, headB: ListNode?): ListNode? {
// Add your code here to find the intersection node
}
fun main() {
// Build linked lists and test the function
}
Run Code?
Click Run Button to view compiled output
28. Create a function to check if a number is a happy number.
Required Input:
A hardcoded number, e.g., 19.
Expected Output:
19 is a happy number
Code In Kotlin
fun isHappyNumber(num: Int): Boolean {
// Add your code here to check if the number is happy
}
fun main() {
val num = 19
if (isHappyNumber(num)) {
println("$num is a happy number")
} else {
println("$num is not a happy number")
}
}
Run Code?
Click Run Button to view compiled output
29. Write a function to transpose a matrix.
Required Input:
A hardcoded matrix:
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Expected Output:
1 4 7
2 5 8
3 6 9
Code In Kotlin
fun transposeMatrix(matrix: Array<Array<Int>>): Array<Array<Int>> {
// Add your code here to transpose the matrix
}
fun main() {
val matrix = arrayOf(
arrayOf(1, 2, 3),
arrayOf(4, 5, 6),
arrayOf(7, 8, 9)
)
val transposed = transposeMatrix(matrix)
transposed.forEach { println(it.joinToString(" ")) }
}
Run Code?
Click Run Button to view compiled output
30. Implement a function to remove duplicates from a sorted linked list.
Required Input:
A sorted linked list: 1 -> 1 -> 2 -> 3 -> 3.
Expected Output:
1 -> 2 -> 3 -> null
Code In Kotlin
class ListNode(val value: Int) {
var next: ListNode? = null
}
fun removeDuplicates(head: ListNode?): ListNode? {
// Add your code here to remove duplicates
}
fun main() {
// Build linked list and test the function
}
Run Code?
Click Run Button to view compiled output