Category: java

  • Data Structures & Algorithms in Java – Dynamic Programming – Decode Ways

    Problem: If A is encoded as 1 , B as 2 and so on until Z as 26 , then find the number of ways a given encoded string can be decoded. For example: The input “121” can be decoded as 1, 2 ,1 (ABA) or 12 1(LA) or 1 21 (AU) So the output…

  • Data Structures and Algorithms in Java – Dynamic Programming – Combination Sum

    Problem: Given an array of numbers and a target number , find in how many ways the numbers in the input array can be summed up to form the target number. For example, Given the input: [1,2,3] and the target 4 The output should be 7 since : 1 + 1 + 1 + 1…

  • Data Structures & Algorithms in Java – Dynamic Programming – Jump Game

    Problem: Given an array of numbers which indicate how many positions maximum you can jump from that index position , find out if you can reach the end of the array from the beginning. For example: For the input [2,3,1,1,4] You can jump maximum 2 positions from index 0, 3 positions from index 1 ,…

  • Data Structures & Algorithms in Java – Dynamic Programming – House Robber II

    Problem: A robber goes to rob in a series of houses built next to each other forming a circle. So the first house and last house are next to each other. The robber cannot steal from consecutive houses as it would trigger an alarm. Find the maximum money the robber can steal. Input: [2,3,2] Each…

  • Data Structures & Algorithms in Java – Dynamic Programming – House Robber

    Problem: A robber goes to rob in a series of houses built next to each other. He can’t rob from two consecutive houses , this will trigger an alarm. How much maximum money can he steal ? Input: The array [1,2,3,1] represents money in each house starting from the first index. Output: Maximum amount the…

  • Datastructures & Algorithms in Java – Dynamic Programming – Word Break

    Problem: Given a word , find if the word could be formed by joining words together from a given dictionary. Example: The word “datastructure” can be formed using the words from the dictionary: [“data”,”algorithms”,”structure”] So ,the output is true. For the word “breadandbiscuit” and the dictionary [“and”,”bis”,”bread”] the output is false Assumptions: The words and…

  • What is “effectively final” in Java?

    Let’s say you don’t want a variable value to be changed once it is initialized. How to achieve this in Java? By declaring the variable as final. Now the variable becomes a constant and the compiler will complain if it is modified anywhere. There is one more concept of “final” in Java , which is…

  • Are we using mutable objects too much?

    By default any object you create in java is mutable. And so we end up using mutable objects more often. But Java team themselves suggest to use Immutable objects more. From https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html : Maximum reliance on immutable objects is widely accepted as a sound strategy for creating simple, reliable code One reason why developers wouldn’t…

  • How to write a generic REST client in Spring Boot?

    We live in the world of microservices. REST APIs are all over and they communicate with each other. To communicate with each other in the Spring World , they use RestTemplate or Spring WebClient or Java’s own HttpClient(Java 11) or any other third party libraries. We call them REST clients. It will be nice if…

  • Java 21 – Pattern Matching for Switch

    Switch statement in java has gone through a rapid evolution since Java 7. You could compare only integers until Java 7. And then Java 8 allowed you to compare strings and enums as well. And then Java 12 introduced a flurry of new features: Java 13 later introduced yield keyword to be used instead of…