Posts

Showing posts from February, 2021

Kotlin Language Features Related to Null Handling

Any software engineer with a Java background would find the null handling features in the Kotlin language interesting. Let's summarize this topic with some examples. Nullable types: In Kotlin, types are non-nullable by default. If you want a variable to be able to hold a null value, you need to explicitly declare its type as nullable using the Type? syntax. For example, String? denotes a nullable string, while String represents a non-nullable string. Safe calls (?.): Kotlin introduces the safe call operator (?.) for handling nullable types. It allows you to safely invoke a method or access a property on a nullable object. If the object is null, the expression returns null instead of throwing a NullPointerException. Example: data class Person(val name: String, val age: Int, val address: String?) fun main() {     // Create a person with a nullable address     val person1 = Person("John Doe", 25, "123 Main Street")     val person2 = Person("Jane Doe", 30,

swapLexOrder: Finding lexicographically largest string

The following coding question is really amazing: Given a string  str  and array of  pairs  that indicates which indices in the string can be swapped, return the  lexicographically largest  string that results from doing the allowed swaps. You can swap indices any number of times. Example For  str = "abdc"  and  pairs = [[1, 4], [3, 4]] , the output should be swapLexOrder(str, pairs) = "dbca" . By swapping the given indices, you get the strings:  "cbda" ,  "cbad" ,  "dbac" ,  "dbca" . The lexicographically largest string in this list is  "dbca" . Input/Output [execution time limit] 3 seconds (java) [input] string str A string consisting only of lowercase English letters. Guaranteed constraints: 1 ≤ str.length ≤ 10 4 . [input] array.array.integer pairs An array containing pairs of indices that can be swapped in  str  (1-based). This means that for each  pairs[i] , you can swap elements in  str  that have the indices  p

Popular posts from this blog

Trie Data Structure and Finding Patterns in a Collection of Words

swapLexOrder: Finding lexicographically largest string

A Graph Application in Java: Using WordNet to Find Outcast Words