Sunday, October 28, 2018

Can you write an algorithm to check if a string is rotation of each other or not ?

Problem statement:
write an algorithm to check if a string is a rotation of each other or not!!
  1. public class CheckStringRotation {
  2.     public static void main(String args[]) {
  3.         String str1 = "ishaan";
  4.         String str2 = "aanish";
  5.         String temp = str1.concat(str1);
  6.         if (temp.contains(str2)) {
  7.             System.out.println(str2 + " is a rotation of " + str1);
  8.         } else {
  9.             System.out.println(str2 + " is not a rotation of " + str1);
  10.         }
  11.     }
  12. }
Output:
aanish is the rotation of ishaan

Saturday, October 27, 2018

what do you mean by Cryptography ?

Cryptography or cryptology (from Ancient Greek: kryptós "hidden, secret"; graphein, "to write") is the practice and study of techniques for secure communication in the presence of third parties called adversaries. More generally, cryptography is about constructing and analyzing protocols that prevent third parties or the public from reading private messages; various aspects in information security such as data confidentiality, data integrity, authentication, and non-repudiation are central to modern cryptography. Modern cryptography exists at the intersection of the disciplines of mathematics, computer science, electrical engineering, communication science, and physics. Applications of cryptography include electronic commerce, chip-based payment cards, digital currencies, computer passwords, and military communications.

Cryptography prior to the modern age was effectively synonymous with encryption, the conversion of information from a readable state to apparent nonsense.

what do you mean by Blockchain?

A blockchain, is a growing list of records, called blocks, which are linked using cryptography. Each block contains a cryptographic hash of the previous block, a timestamp, and transaction data (generally represented as a merkle tree root hash).

Tuesday, October 16, 2018

what do you mean by synchronized block in java?

Problem statement: Can you explain me about synchronized block in java?

Synchronized block: A block which contains synchronized keyword that is called synchronized block.

#Synchronized block:
  1. class Institute {
  2.     public void classRoom(String facultyName) {
  3.         synchronized (Institute.class) {
  4.             for (int i = 0; i < 10; i++)
  5.                 System.out.println(i + " .class taken by " + facultyName);
  6.             try {
  7.                 Thread.sleep(1000);
  8.             } catch (InterruptedException e) {
  9.                 e.printStackTrace();
  10.             }
  11.         }
  12.     }
  13. }
  14. class MyThread extends Thread {
  15.     Institute inst;
  16.     String factName;
  17.     @Override
  18.     public void run() {
  19.         inst.classRoom(factName);
  20.     }
  21.     MyThread(Institute inst, String name) {
  22.         this.inst = inst;
  23.         this.factName = name;
  24.     }
  25. }
  26. public class SynchronizedExe {
  27.     public static void main(String args[]) {
  28.         Institute inst1 = new Institute();
  29.         Institute inst2 = new Institute();
  30.         MyThread t1 = new MyThread(inst1, "Madhusmita");
  31.         MyThread t2 = new MyThread(inst2, "Ishaan");
  32.         t1.start();
  33.         t2.start();
  34.     }
  35. }
Output:
0 .class taken by Ishaan
1 .class taken by Ishaan
2 .class taken by Ishaan
3 .class taken by Ishaan
4 .class taken by Ishaan
5 .class taken by Ishaan
6 .class taken by Ishaan
7 .class taken by Ishaan
8 .class taken by Ishaan
9 .class taken by Ishaan
0 .class taken by Madhusmita
1 .class taken by Madhusmita
2 .class taken by Madhusmita
3 .class taken by Madhusmita
4 .class taken by Madhusmita
5 .class taken by Madhusmita
6 .class taken by Madhusmita
7 .class taken by Madhusmita
8 .class taken by Madhusmita
9 .class taken by Madhusmita

#Without synchronized block
  1. class Institute {
  2.     public void classRoom(String facultyName) {
  3.         for (int i = 0; i < 10; i++)
  4.             System.out.println(i + " .class taken by " + facultyName);
  5.         try {
  6.             Thread.sleep(1000);
  7.         } catch (InterruptedException e) {
  8.             e.printStackTrace();
  9.         }
  10.     }
  11. }
  12. class MyThread extends Thread {
  13.     Institute inst;
  14.     String factName;
  15.     @Override
  16.     public void run() {
  17.         inst.classRoom(factName);
  18.     }
  19.     MyThread(Institute inst, String name) {
  20.         this.inst = inst;
  21.         this.factName = name;
  22.     }
  23. }
  24. public class SynchronizedExe {
  25.     public static void main(String args[]) {
  26.         Institute inst1 = new Institute();
  27.         Institute inst2 = new Institute();
  28.         MyThread t1 = new MyThread(inst1, "Madhusmita");
  29.         MyThread t2 = new MyThread(inst2, "Ishaan");
  30.         t1.start();
  31.         t2.start();
  32.     }
  33. }
Output:
0 .class taken by Madhusmita
1 .class taken by Ishaan
2 .class taken by Madhusmita
3 .class taken by Ishaan
4 .class taken by Madhusmita
5 .class taken by Ishaan
---------------------------------------
#With synchronized method
  1. class Institute {
  2.     synchronized public void classRoom(String facultyName) {
  3.         for (int i = 0; i < 10; i++)
  4.             System.out.println(i + " .class taken by " + facultyName);
  5.         try {
  6.             Thread.sleep(1000);
  7.         } catch (InterruptedException e) {
  8.             e.printStackTrace();
  9.         }
  10.     }
  11. }
  12. class MyThread extends Thread {
  13.     Institute inst;
  14.     String factName;
  15.     @Override
  16.     public void run() {
  17.         inst.classRoom(factName);
  18.     }
  19.     MyThread(Institute inst, String name) {
  20.         this.inst = inst;
  21.         this.factName = name;
  22.     }
  23. }
  24. public class SynchronizedExe {
  25.     public static void main(String args[]) {
  26.         Institute inst1 = new Institute();
  27.         Institute inst2 = new Institute();
  28.         MyThread t1 = new MyThread(inst1, "Madhusmita");
  29.         MyThread t2 = new MyThread(inst2, "Ishaan");
  30.         t1.start();
  31.         t2.start();
  32.     }
  33. }
Output:
0 .class taken by Madhusmita
0 .class taken by Ishaan
1 .class taken by Madhusmita
1 .class taken by Ishaan
2 .class taken by Madhusmita
2 .class taken by Ishaan

Sunday, October 14, 2018

What is JDK ahead of time [AOT] compilation ?

In computer science, ahead-of-time (AOT) compilation is the act of compiling a higher-level programming language such as C or C++, or an intermediate representation such as Java bytecode or .NET Framework Common Intermediate Language (CIL) code, into a native (system-dependent) machine code so that the resulting binary file can execute natively.

AOT produces machine optimized code, just like a standard native compiler. The difference is that AOT transforms the bytecode of an extant virtual machine (VM) into machine code

How do you generate unique alphanumeric character in java?

Problem statement:
write an algorithm to generate alphanumeric characters using java.

public class GenerateUUID {
    public static void main(String args[]) {
        for (int i = 0; i < 1000000; i++) {
            UUID uuid1 = UUID.randomUUID();
            System.out.println(uuid1);  // 8-4-4-4-12 (36 char/32 + 4 )
        }
    }
}
Output:
889255d3-8179-4fd0-b870-fa7246e7b306
c46cc028-503d-469f-8aac-defe8b80a4df
d0fbf928-2c01-466f-86c6-6bf4c492799c
fa7bbabc-194b-4331-95fc-8676414858d5
dddca4af-83fe-44c4-8e65-c375146414bb
35681f4f-e3a9-4a45-85f0-d7ecccf6c2df
a577f324-e33f-4396-be9b-7b80cfe821de
----------------------------------------------
----------------------------------------------
----------------------------------------------

Note: we must create new instance on every single iteration withing a loop, then only it could be, else single instance will get looped over the multiple times.
*************************************************
public class GenerateUUID {
    public static void main(String args[]) {
        UUID uuid1 = UUID.randomUUID();
        for (int i = 0; i < 1000000; i++) {
            System.out.println(uuid1);  // 8-4-4-4-12 (36 char / 32 + 4 hyphen)
        }
    }
}
Output:
2bb222ac-23c0-488a-83c8-75c5c9bc5cdd
2bb222ac-23c0-488a-83c8-75c5c9bc5cdd
2bb222ac-23c0-488a-83c8-75c5c9bc5cdd
2bb222ac-23c0-488a-83c8-75c5c9bc5cdd
2bb222ac-23c0-488a-83c8-75c5c9bc5cdd
2bb222ac-23c0-488a-83c8-75c5c9bc5cdd
2bb222ac-23c0-488a-83c8-75c5c9bc5cdd
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------

How do you write an algorithm to generate unique 17 digit number in java ?

Problem statement:
write an algorithm to generate 17 digit unique number.

public class GenerateUniqueRandNums {
    public static void main(String args[]) {
        for (int count = 0; count < 1000000; count++) {
            long unqNum = System.nanoTime();
            System.out.println("unique: " + unqNum* 100);
        }
    }
}
Note: we must create new instance on every single iteration withing a loop, then only it could be, else single instance will get looped over the multiple times.
Output:
unique: 2750731359372300
unique: 2750731485912300
unique: 2750731491573500
unique: 2750731496781900
unique: 2750731501673200
unique: 2750731569698600
--------------------------------
--------------------------------
--------------------------------
*************************************************
public class GenerateUniqueRandNum {
    public static void main(String args[]) {
        long unqNum = System.nanoTime();
        for (int count = 0; count < 1000000; count++) {
            System.out.println("unique: " + unqNum* 100);
        }
    }

}
Output:
unique: 2946684697352300
unique: 2946684697352300
unique: 2946684697352300
unique: 2946684697352300
unique: 2946684697352300

unique: 2946684697352300
--------------------------------
--------------------------------
--------------------------------

What is GraalVM ?

GraalVM is an extension of the Java Virtual Machine[JVM] to support more languages and execution modes. The Graal project includes a new high performance Java compiler, itself called Graal, which can be used in a just-in-time configuration on the HotSpot VM, or in an ahead-of-time [AOT] configuration on the SubstrateVM.

One objective of Graal is to improve the performance of Java virtual machine-based languages to match the performance of native languages. Another goal, dubbed "Project Metropolis" or "Java-on-Java", is to implement a substitute for the substantial use of C++ within HotSpot. A third goal is to allow freeform mixing of code from any programming language in a single program, billed as "polyglot applications".

GraalVM is a universal virtual machine for running applications written in JavaScript, Python 3, Ruby, R, JVM-based languages like Java, Scala, Kotlin, and LLVM-based languages such as C and C++.

GraalVM removes the isolation between programming languages and enables interoperability in a shared runtime. It can run either standalone or in the context of OpenJDK, Node.js, Oracle Database, or MySQL.

Saturday, October 13, 2018

Can you generate random specific digit number in java?

Problem statement: Write an algorithm to generate random specific digit ( meaning 17 digit or 18 digit or 5 digit or any number of digit) number in java.
import java.util.Random;
public class GenerateRandomNumber {
    public static void main(String args[]) {
        randomNumber();
    }
    private static void randomNumber() {
        for (int count = 0; count < 10; count++) {
            Random r = new Random();
            final float aFloat = r.nextFloat();
            final double pow = Math.pow(10, 17);
            final long round = Math.round(aFloat * pow);
            System.out.println(round);
        }
    }
}
Output:
12574732303619384
50630849599838256
37673717737197872
15234947204590
16874015331268310
39088690280914304
70018148422241208
57346004247665408
25875741243362428

84610998630523680

Friday, October 12, 2018

How do you remove the special characters from a string using Regex in java ?

Problem statement:
can you remove the special character from a string using regex in java?

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RemoveSpecialCharacter{
      public static void main(String args[]){
      String c = "p$82-a/*@#^&873";
     
      Pattern pt = Pattern.compile("^a-zA-Z0-9");
      Matcher match = pt.matcher(c);
      while(match.find()){
        String s = match.group();
        c = c.replaceAll("\\"+s, "");
       }
        System.out.println(c);
      }
}
Output:
p82a873

Thursday, October 11, 2018

Do you know UUID class in java?

Problem statement:
What do you mean by UUID class in java?
UUID [Universally Unique Identifier]
A class that represents an immutable universally unique identifier (UUID). A UUID represents a 128-bit value.
Ex - 
 0xFFFFFFFF00000000 time_low
 0x00000000FFFF0000 time_mid
 0x000000000000F000 version
 0x0000000000000FFF time_hi

can string class be extended

Problem statement:
can string class be extended

No ! 
As string class is a final class, hence this cannot be extended.

What are seven layers of networking?

Problem statement:
What are seven layers of networking?

How can you cut a rectangular cake in 8 symmetric pieces in three cuts?

Problem statement:
How can you cut a rectangular cake in 8 symmetric pieces in three cuts?

In a dark room,there is a box of 18 white and 5 black gloves. You are allowed to pick one and then you are allowed to keep it and check it outside. How many turns do you need to take in order for you to find a perfect pair?

Problem statement:
In a dark room,there is a box of 18 white and 5 black gloves. You are allowed to pick one and then you are allowed to keep it and check it outside. How many turns do you need to take in order for you to find a perfect pair?

Wednesday, October 3, 2018

What are the applications of Topological Sorting in graph?

Problem statement: 
what are the uses of Topological Sorting in graph?
  1. Representing course prerequisites
  2. In detecting deadlocks
  3. Evaluating formulae in spreadsheet
  4. Pipeline of computing jobs
  5. Checking for symbolic link loop

What do you mean by Topological Sort in graph ?

Problem statement: 
what is Topological sort in graph data structure & algorithm?

Topological sort is an ordering of vertices in a directed acyclic  graph [DAG] in which each node comes before all nodes to which it has outgoing edges.
For example:
Consider the course prerequisite structure at universities. A directed edge (v,w) indicates that course v must be completed before course w. Every DAG may have one or more topological orderings. Topological sort is not possible if the graph has a cycle, since for two vertices v & w on the cycle, v precedes w & w precedes v.

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...