Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Oct 4, 2025

Java 21 Virtual Threads: Revolutionizing Concurrency and Migrating from Traditional Threads

Java 21 marks a pivotal moment in JVM history — the arrival of Virtual Threads, a game-changing feature from Project Loom.

For decades, Java’s concurrency model has relied on OS-level threads, each carrying significant memory and context-switch overhead. That model struggled under modern workloads — think thousands of concurrent web requests, reactive microservices, and async I/O.

With Virtual Threads, Java finally delivers structured concurrency and massive scalability without abandoning its core threading APIs.

In this post, you’ll learn:

  • How Virtual Threads work internally

  • How to migrate from traditional threads

  • Real-world code comparisons and performance benchmarks

  • Best practices for production-ready adoption

Sep 16, 2025

Concurrency vs Parallelism: Understanding the Difference

  • In the world of software development, we often hear the terms concurrency and parallelism, sometimes used interchangeably. While they are related, they are not the same—and knowing the difference is key to writing efficient and scalable programs

Nov 29, 2023

Mastering Design Pattern : Navigating Common Pitfalls with & without Design Pattern Code Solutions - Part 3🛡️

  • In the dynamic realm of software development, mastering design patterns is paramount for crafting robust, scalable, and maintainable solutions. While design patterns provide reusable and proven solutions to recurring problems, their effective implementation requires a nuanced understanding to steer clear of potential pitfalls. 
  • Design patterns encapsulate best practices, offering elegant and proven solutions to recurring problems. Whether it's the structural elegance of the Singleton pattern, the flexibility of the Strategy pattern, or the composability of the Composite pattern, each design pattern addresses specific concerns within the software development lifecycle.
  • This post delves into the art of navigating these challenges, both with and without the aid of design pattern code solutions.

Nov 28, 2023

Mastering Microservices: Navigating Common Pitfalls with Bad Code to Good Code Solutions - Part 2🛡️

  • Dive into the world of microservices with examples of bad code practices and their corresponding good code solutions. 
  • Each example focuses on common pitfalls encountered in microservice development, addressing issues such as performance, scalability, and maintainability. 
  • Learn how to optimize your microservices architecture by exploring practical scenarios, understanding the drawbacks of bad code, and implementing effective solutions to enhance the overall robustness of your applications.

Nov 24, 2023

Java Performance: Avoiding Pitfalls and Embracing Best Practices - Part 1🛡️

  • In the realm of software development, performance is a crucial aspect that directly impacts the user experience and overall efficiency of applications. 
  • Java, a widely used programming language, offers a robust framework for building high-performance applications. However, certain programming practices can inadvertently introduce performance bottlenecks, hindering the application's responsiveness and scalability.
  • This article delves into the world of Java performance, exploring both bad and good practices through illustrative examples. By understanding the implications of these practices, developers can make informed decisions to optimize their code and achieve optimal performance.

Dec 1, 2022

Easy_Question27 : 3Sum - Given an integer array nums, return all the triplets

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

Notice that the solution set must not contain duplicate triplets.

Nov 15, 2022

Java memory arguments for Containers

  • When you are running your Java application in physical servers, you would have been using ‘-Xmx’ JVM argument to specify the Java heap size. 
  • If you are porting your application to Containers, you might be wondering how to configure Java heap size in the container’s world?

Nov 9, 2022

StackoverflowError: Causes & Solutions

StackOverFlowError is one of the commonly confronted JVM errors. In this blog post, let's learn the inner mechanics of thread stacks, reasons that can trigger StackOverFlowError and potential solutions to address this error.

Nov 8, 2022

Benefits of setting initial and maximum memory size to the same value

 

  • When we launch applications, we specify the initial memory size and maximum memory size. For the applications that run on JVM (Java Virtual Machine), initial and maximum memory size is specified through ‘-Xms’ and ‘-Xmx’ arguments. 
  • If Java applications are running on containers, it’s specified through ‘-XX: InitialRAMPercentage’ and ‘-XX: MaxRAMPercentage’ arguments. Most enterprises set the initial memory size to a lower value than the maximum memory size. 
  • As opposed to this commonly accepted practice, setting the initial memory size the same as the maximum memory size has certain ‘cool’ advantages.

May 19, 2022

Easy_Question25 : Majority Element

Given an array nums of size n, return the majority element.

The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. 

Easy_Question24: Convert BST to Greater Tree

Given the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.

What is a greater sum tree: 

  • A greater sum tree is a tree in which every node contains the sum of all the nodes which are greater than the node.  see the example below

Easy_Question23 : Divisor game

Given an integer N and two players, A and B are playing a game. On each player’s turn, that player makes a move by subtracting a divisor of current N (which is less than N) from current N, thus forming a new N for the next turn.

May 18, 2022

Easy_Question22 : Leaf-Similar Trees

Two binary trees are considered leaf-similar if their leaf value sequence is the same.

Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.

Easy_Question21 :Monotonic Array

An array is monotonic if it is either monotone increasing or monotone decreasing.

An array nums is monotone increasing if for all i <= jnums[i] <= nums[j]. An array nums is monotone decreasing if for all i <= jnums[i] >= nums[j].

Given an integer array nums, return true if the given array is monotonic, or false otherwise.

Easy_Question20 : Painting Fence

There is a fence with n posts, each post can be painted with one of the k colors.


find out the number of ways of painting the fence such that at most 2 adjacent posts have the same color.

Note: n and k are non-negative integers.

Easy_Question19 : Merge Two Binary Trees

  • You are given two binary trees root1 and root2.
  • Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree.
  • Return the merged tree.
  • Note: The merging process must start from the root nodes of both trees.

Example:  
Input: 
     Tree 1            Tree 2                  
       2                 3                             
      / \               / \                            
     1   4             6   1                        
    /                   \   \                      
   5                     2   7                  

Output: Merged tree:
         5
        / \
       7   5
      / \     \
     5   2   7 

May 17, 2022

Easy_Question18 : Is Subsequence

  • A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. 

  • (ie, "ace" is a subsequence of "abcde" while "aec" is not).

Example 1:
  • input: s = "abc", t = "ahbgdc"
  • Output: true
Example 2:
  • Input: s = "axc", t = "ahbgdc"
  • Output: false

Easy_Question17 :Minimize cost of painting N houses such that adjacent houses have different colors

  • There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.
  • The cost of painting each house with a certain color is represented by a n x 3 cost matrix. For example, costs[0][0] is the cost of painting house 0 with color red; costs[1][2] is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.

  • Given an integer N and a 2D array cost[][3], where cost[i][0]cost[i][1], and cost[i][2] is the cost of painting ith house with colors redblue, and green respectively, the task is to find the minimum cost to paint all the houses such that no two adjacent houses have the same color.
Examples:
Input: N = 3, cost[][3] = {{14, 2, 11}, {11, 14, 5}, {14, 3, 10}}
Output: 10

Explanation: 
  • Paint house 0 as blue. Cost = 2.
  • Paint house 1 as green. Cost = 5. 
  • Paint house 2 as blue. Cost = 3.
  • Therefore, the total cost = 2 + 5 + 3 = 10.
Input: N = 2, cost[][3] = {{1, 2, 3}, {1, 4, 6}}
Output: 3

Easy_Question16 : Shortest Unsorted Continuous Subarray

Given an integer array nums, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.

Return the shortest such subarray and output its length.

Easy_Question15 : Lowest Common Ancestor in a Binary Search Tree

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

You may also like

Kubernetes Microservices
Python AI/ML
Spring Framework Spring Boot
Core Java Java Coding Question
Maven AWS