#1. Why convert a Date to String & a String back to Date
(a) Convert a String input from say a file date, so that you can perform operations like
1) Adding 5 days to the date.
2) Comparing a date like before,
…
#1. Why convert a Date to String & a String back to Date
(a) Convert a String input from say a file date, so that you can perform operations like
1) Adding 5 days to the date.
2) Comparing a date like before,
…
This is the reverse of Converting an Array/List to BST in Java. It is a very common task to convert a collection type A to B as a developer. More examples to practice: Converting from A to B There are 3 ways to traverse a tree to flatten it to...
Q. What is a BST? A. BST stands for Binary Search Tree, sometimes called ordered or sorted binary trees. This is a type of data structures that store “items” such as numbers, names etc in memory. A BST allows fast lookup, addition and removal of items, … Read more ›...
#1. Does Java have a “Money” class? No. This will change in Java 9 with the “Money API”. JSR 354 defines a new Java API for working with Money and Currencies. #2. What are the 2 potential pitfalls in working with money? … Read more ›...
Most feasible way to handle consistency across microservices is via eventual consistency. This model doesn’t enforce distributed ACID transactions across microservices. Event sourcing is an event-centric approach to business logic design and persistence. It favours to use some mechanisms of ensuring that the system would be eventually consistent at some...
Q1. Can you write a function to determine the nth Fibonacci number? The Fibonacci numbers under 2000 are : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597. Where the zeroth number being 0, first number being 1, … Read more...
Complimenting Fibonacci number coding – iterative and recursive approach, we can improve performance by caching. If you run this
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class RecursiveFibonacci { public int fibonacci(int n) { if (n == 0 || n == 1) return n; System.out.println("evaluating fibonacci(" + n + ")"); return fibonacci(n - 2) + fibonacci(n - 1); } public static void main(String[] args) { int nThfibonacciNo = new RecursiveFibonacci().fibonacci(5); System.out.println(nThfibonacciNo); } } |
Output
|
1 2 3 4 5 6 7 8 9 |
evaluating fibonacci(5) evaluating fibonacci(3) evaluating fibonacci(2) evaluating fibonacci(4) evaluating fibonacci(2) evaluating fibonacci(3) evaluating fibonacci(2) 5 |
and you can see “fibonacci(3)” is repeated 2 times, “fibonacci(2)” is repeated 3 times, and so on. If you pick a larger number like 21, … Read more...
Q1. Given an array of integers, find two numbers such that they add up to a specific target number? For example, Given numbers: {2, 3, 8, 7, 5} Target number: 9 Result: 2 and 7 A1. Solution 1: Store processed numbers in a set. … Read more ›...
The SinglyLinkedList was created in the post “LinkedList creating from scratch in Java“. This extends “SinglyLinkedList” post. Q. How to find the middle node of a linked list in a single pass? A.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package linked.list; public class LinkedListTest { public static void main(String args[]) { SinglyLinkedList<Integer> linkedList = new SinglyLinkedList<Integer>(); linkedList.add(1); linkedList.add(2); linkedList.add(3); printMiddleNodeInsinglePass(linkedList); } public static void printMiddleNodeInsinglePass(SinglyLinkedList<Integer> linkedList) { Node<Integer> current = linkedList.getHead(); int length = 0; Node<Integer> middle = linkedList.getHead(); //Loop until last element is reached while (current.getNext() != null) { length++; if (length % 2 == 0) { middle = middle.getNext(); } current = current.getNext(); } if (length % 2 == 1) { middle = middle.getNext(); } System.out.println("length of SinglyLinkedList: " + (length+1)); System.out.println("middle element of SinglyLinkedList : " + middle); } } |
Output:
|
1 2 3 |
length of SinglyLinkedList: 3 middle element of SinglyLinkedList : data=2, next=data=3, next=null |
… Read more ›...
Q. Write a method which takes the parameters (int[ ] inputNumbers, int sum) and checks input to find the pair of integer values which totals to the sum. If found returns true, else returns false? Considerations: Should it work for negative integers? … Read more ›...
Firstly, understand the problem, and write down any considerations & assumptions. Next write pseudocode if that helps. Considerations 1) Pre-codnition check for null or empty input. 2) Loop through the input string, and store each “character” as a key in a map with the value being the “character count”. …...
Q. Can you write code to identify missing numbers in a given array of numbers?
Solution 1: Assuming that the given numbers are in order
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
public class MissingNumber { public static void main(String[] args) { int numbers[] = { 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18 }; new MissingNumber().findMissingNumbers(numbers, 0, 20); } private void findMissingNumbers(int[] input, int first, int last) { printMissingNumbersFront(input, first); printMissingNumbersInTheMiddle(input, last); printMissingNumbersBack(input, last); } /** * before 2 * @param input * @param first */ private void printMissingNumbersFront(int[] input, int first) { //0 and 1 are less than 2 as per the above input for (int i = first; i < input[0]; i++) { System.out.println(i); } } /** * after 18 * @param input * @param last */ private void printMissingNumbersBack(int[] input, int last) { // 1 + the last element value is 19. 19 to 20 are less than or equal to the last value of 20 for (int i = 1 + input[input.length - 1]; i <= last; i++) { System.out.println(i); } } /** * between 2 and 18 * @param input * @param last */ private void printMissingNumbersInTheMiddle(int[] input, int last) { //for a given index i, index [i-1]+1 should be equal if present. for (int i = 1; i < input.length; i++) { //e.g. for input[1] = 3, j=1+2 //input[2] = 4, j=1+3 //input[3] = 6, j=4+1, Oops 5 is less than 6 so print //input[4] = 7, j=6+1 //if index[i] < index[i-1] + 1, then number index[i-1] + 1 is missing for (int j = 1+input[i-1]; j < input[i]; j++) { System.out.println(j); } } } } |
Output:
|
1 2 3 4 5 6 7 |
0 1 5 16 17 19 20 |
The above solution assumes that the numbers are in order (i.e.
…
Q. Can you write code to output the perfect number between a given range? Definition: A perfect number is a positive integer that is equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, … Read more ›...
I have a good news, I got job offer. I like to share with you from my bottom of my heart feeling thankful to you.