11. Write a Kotlin program to reverse an integer without converting it to a string.
Required Input:
A hardcoded integer: 12345
Expected Output:
Reversed number: 54321
Code In Kotlin
fun reverseNumber(num: Int): Int {
// Add your code here to reverse the integer
}
fun main() {
val number = 12345
println("Reversed number: ${reverseNumber(number)}")
}
Run Code?
Click Run Button to view compiled output
12. Write a function to check if a string is a rotation of another string.
Required Input:
Two strings: str1 = "abcd", str2 = "cdab"
Expected Output:
"cdab is a rotation of abcd"
Code In Kotlin
fun main() {
val sentence = "Kotlin programming is fun"
// Add your code here to find the longest word
}
Run Code?
Click Run Button to view compiled output
13. Write a function to reverse words in a given string.
Required Input:
A string: "Hello World"
Expected Output:
Reversed string: "World Hello"
Code In Kotlin
fun isPrime(num: Int): Boolean {
// Add your code here to check if a number is prime
}
fun sumOfPrimes(limit: Int): Int {
// Add your code here to calculate the sum of primes
}
fun main() {
val limit = 10
println("The sum of prime numbers below $limit is: ${sumOfPrimes(limit)}")
}
Run Code?
Click Run Button to view compiled output
14. Write a function to count the frequency of each character in a string.
Required Input:
A string: "hello"
Expected Output:
Character frequencies: {h=1, e=1, l=2, o=1}
Code In Kotlin
fun generatePascalsTriangle(rows: Int): List<List<Int>> {
// Add your code here to generate Pascal's Triangle
}
fun main() {
val rows = 5
val triangle = generatePascalsTriangle(rows)
triangle.forEach { println(it.joinToString(" ")) }
}
Run Code?
Click Run Button to view compiled output
15. Write a function to find the longest substring without repeating characters.
Required Input:
A string: "abcabcbb"
Expected Output:
Longest substring: "abc"
Code In Kotlin
fun maxDifference(arr: Array<Int>): Int {
// Add your code here to find the maximum difference
}
fun main() {
val array = arrayOf(2, 3, 10, 6, 4, 8, 1)
println("The maximum difference is: ${maxDifference(array)}")
}
Run Code?
Click Run Button to view compiled output
16. Write a function to find the longest palindromic substring in a given string.
Required Input:
A string: "babad"
Expected Output:
Longest palindromic substring: "bab"
Code In Kotlin
fun isSymmetric(matrix: Array<Array<Int>>): Boolean {
// Add your code here to check if the matrix is symmetric
}
fun main() {
val matrix = arrayOf(
arrayOf(1, 2, 3),
arrayOf(2, 4, 5),
arrayOf(3, 5, 6)
)
if (isSymmetric(matrix)) {
println("The matrix is symmetric")
} else {
println("The matrix is not symmetric")
}
}
Run Code?
Click Run Button to view compiled output
17. Write a function to check if a string is an anagram of another string.
Required Input:
Two strings: str1 = "listen", str2 = "silent"
Expected Output:
True (since they are anagrams)
Code In Kotlin
fun longestIncreasingSubsequence(arr: Array<Int>): List<Int> {
// Add your code here to find the longest increasing subsequence
}
fun main() {
val array = arrayOf(10, 22, 9, 33, 21, 50, 41, 60)
println("Longest increasing subsequence: ${longestIncreasingSubsequence(array)}")
}
Run Code?
Click Run Button to view compiled output
18. Create a program to find the intersection of two arrays.
Required Input:
Two arrays: [1, 2, 3, 4] and [3, 4, 5, 6]
Expected Output:
Intersection: [3, 4]
Code In Kotlin
fun findIntersection(arr1: Array<Int>, arr2: Array<Int>): List<Int> {
// Add your code here to find the intersection
}
fun main() {
val arr1 = arrayOf(1, 2, 3, 4)
val arr2 = arrayOf(3, 4, 5, 6)
println("Intersection: ${findIntersection(arr1, arr2)}")
}
Run Code?
Click Run Button to view compiled output
19. Write a function to find all pairs in an array whose sum is equal to a given target.
Required Input:
Array: [1, 2, 3, 4, 5], Target: 5
Expected Output:
Pairs: [(2, 3), (1, 4)]
Code In Kotlin
fun findPairsWithSum(arr: Array<Int>, target: Int): List<Pair<Int, Int>> {
// Add your code here to find pairs with the given sum
}
fun main() {
val array = arrayOf(1, 2, 3, 4, 5)
val target = 5
println("Pairs: ${findPairsWithSum(array, target)}")
}
Run Code?
Click Run Button to view compiled output
20. Implement a program to find the majority element (element that appears more than n/2 times) in an array.
Required Input:
Array: [2, 2, 1, 1, 2]
Expected Output:
Majority element: 2
Code In Kotlin
fun findMajorityElement(arr: Array<Int>): Int? {
// Add your code here to find the majority element
}
fun main() {
val array = arrayOf(2, 2, 1, 1, 2)
println("Majority element: ${findMajorityElement(array)}")
}
Run Code?
Click Run Button to view compiled output