Posts

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,

A Nice Explanation of the Public Key Cryptography

Recently I've read the explanation below, which is quite good and easy to understand: Public key cryptography, also known as asymmetric cryptography, is a cryptographic technique that uses a pair of keys, a public key and a private key, for secure communication and encryption. This system is widely used in various applications, including secure communication over the internet, digital signatures, and secure data storage. Let's dive into how public key cryptography works step by step: 1. Key Pair Generation: A user or entity generates a key pair consisting of a public key and a private key. These keys are mathematically related, but it is computationally infeasible to derive the private key from the public key. 2. Public Key Distribution: The user or entity freely distributes their public key to anyone who needs to communicate with them securely. The public key can be shared openly and is used for encryption. 3. Private Key Protection: The private key is kept secret and securely

Trie Data Structure and Finding Patterns in a Collection of Words

Image
 I faced a very hard problem recently about finding substrings based on a collection of patterns.  Example For  words = ["Apple", "Melon", "Orange", "Watermelon"]  and  parts = ["a", "mel", "lon", "el", "An"] , the output should be  findSubstrings(words, parts) = ["Apple", "Me[lon]", "Or[a]nge", "Water[mel]on"] . While  "Watermelon"  contains three substrings from the  parts  array,  "a" ,  "mel" , and  "lon" ,  "mel"  is the longest substring that appears first in the string. Note: Number of words and number of parts can be huge! A brute force method or careless usage of Java String methods would make it impossible to handle a large number of words and parts (patterns). Solution Approach Whenever we want to search patterns inside a string, we should think about the Trie data structure:  https://en.wikipedia.

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

Image Resizing with Directed Graphs: Seam Carving in Java

Image
Seam Carving is an image resizing technique that was discovered in 2007 and it is a feature in Photoshop. We will look at the implementation of this technique using directed graph data structure. Below are some wikipedia links to the underlying data structre: https://en.wikipedia.org/wiki/Directed_acyclic_graph https://en.wikipedia.org/wiki/Shortest_path_problem The goal is to remove a seam (vertical or horizontal) from the image one by one. To calculate a seam, we need to calculate the energies of the pixels. Let's look at the energy method of our SeamCarver class: public class SeamCarver {     private Picture picture; //... public double energy(int x, int y) {         if (x < 0 || x >= width) {             throw new IllegalArgumentException();         }         if (y < 0 || y >= height) {             throw new IllegalArgumentException();         }         if (x == 0 || y == 0 || x == width - 1 || y == height - 1) {             return 1000.;

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

Image
I'll explain our intent with an example: Let's say we have the following words: horse, zebra, cat, bear, table Which one is the outcast? The table.. But how can a computer system find this? We are talking about the whole English language and any word can be in this list. The number of words in the list can be huge, so the solution should use appropriate data structures and algorithms.  We'll use WordNet which is a graph of English words. It contains the semantic relationships between words and our concern is the "is a" relationship. For our example, we know that horse and cat are both animals. "Table" is not an animal and it is the outcast in this situation. But we even have the ability to tell that horse and zebra is very close. Below is a unit test that shows a high level view of our application in Java: public class OutcastTest {     private WordNet wordNet;     private Outcast outcast;     @Before  

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