Skip to main content

Posts

Showing posts from December, 2017

Coding Interview : How to find duplicate characters from String in Java also print the frequency?

The problem statement say that we need to identify how many time a character appear in a string.

Program : 
package net.jubiliation.example;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class CharacterFrequencyExample {
 public static void main(String[] args) {

  String str = "yweeeeerwwesfsfsfsgsgdgxcbgd";

  // convert this String into array of character
  char[] characterArray = str.toCharArray();
  final Map<Character, Integer> charactersMap = new HashMap<>();

  // maintain for each loop to traverse through each element
  for (Character character : characterArray) {
   if (charactersMap.containsKey(character)) {
    int count = charactersMap.get(character);
    count++;
    charactersMap.put(character, count);
   } else {
    charactersMap.put(character, 0);
   }
  }

  // Iterating the map to print the characters and frequency
  for (Entry<Character, Integer> characterEntry : charactersMap.entrySet()) {
   Character character = characterEnt…

Coding Interview : How do you reverse word of a sentence in Java?

Problem statement says that we need to reverse the each word of a given sentence.

Program : 
package net.jubiliation.example;
/**
 * @author Gaurav
 *
 */
public class StringReverseExample {

 public static void reverseWordInMyString(String sentence) {

  String[] words = sentence.split(" ");
  /*
   * Here split() method of String class splits a string in chunks based
   * on the delimiter passed as an argument to it.
   */
  String reversedSentence = "";

  for (int index = 0; index < words.length; index++) {
   String word = words[index];
   String reverseWord = "";

   for (int temp = word.length() - 1; temp >= 0; temp--) {
    /*
     * The charAt() function returns the character at the given position in a string.
     */
    reverseWord = reverseWord + word.charAt(temp);
   }
   reversedSentence = reversedSentence + reverseWord + " ";
  }

  System.out.println(sentence);
  System.out.println(reversedSentence);

 }

 public static void main(String[] args) {

  reverseWord…

Coding Interview : How to calculate factorial using recursion and iteration?

Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example:
4! = 4*3*2*1 = 24
5! = 5*4*3*2*1 = 120
Here, 4! is pronounced as "4 factorial".
The factorial is normally used in Combinations and Permutations (mathematics).
Program : (Using Recursion) package net.jubiliation.example; /**
 * @author Gaurav
 *
 */
public class FactorialExample {
 static int factorialFunction(int number) {
  if (number == 0)
   return 1;
  else
   return (number * factorialFunction(number - 1));
 }
 public static void main(String args[]) {
  int number = 5;// It is the number to calculate factorial
  System.out.println("Factorial of " + number + " is: " + factorialFunction(number));
 }
}
Result : Factorial of 5 is: 120 =========================================================== Program : (Using Iteration) package net.jubiliation.example; /**
 * @author Gaurav
 *
 */
public class FactorialExample {
 static int factorialFunction(int number) {
  int result = 1;
  …

Coding Interview : How to find a missing number in a sorted array?

Here we need to get the following results Sample Input : 1 3 4 5 6 7 8 9
Output : Missing number is 2

Sample Input : 1 2 3 4 5 7 8 9 10
Output : Missing number is 6
Program: /**
 * @author Gaurav
 *
 */
public class MissingNumberPuzzle {  /* main method */
 public static void main(String args[]) {
  int array[] = { 1, 2,3, 4, 5, 6 };
  System.out.println("Missing number is " + getMissingNumber(array));
 }  // Business logic to find missing number
 static int getMissingNumber(int a[]) {
  int temp = a.length;
  for (int index = 0; index < temp; index++)
   if (a[index] != (index + 1))
    return (index + 1);   // If all numbers from 1 to n
  // are present
  return temp + 1;
 }
}

Database and SQL Interview Questions

Can we create indexes for all the column? Truncate vs drop vs delete? What is a key? What is the difference among the Composite key, Candidate key, Primary key  and Foreign key? What is a view? What is the difference between Views & procedures? 2nd highest salary from table? Nth highest salary from table? Select top 5 rows from table? Trigger syntax? procedure syntax? View Syntax? Function syntax? Data base objects? Cursors vs ref cursors? What is Atomic transactions? What is fragment transactions? Why we need self join? How to create Many to Many relationship? How to create One to One relationship? How to create Many to One or One to Many relationship?

Hibernate : Interview Questions

How to configure second level cache?What is hibernate? why we use it over jdbc?How HQL queries are interacting with data base?How HQL queries are converting in to data base specific queries?What is the role of dialect class in hibernate?Difference between cache providers?cascade vs inverse?Why we use criteria in hibernate?Many to many example in hibernatebatch query in hibernateHibernate Lazy loading and early LoadingLoad vs Get in HibernateMerge vs update in hibernateHow exactly inverse will work in hibernate?discriminator column in hibernateSaving an object 2 times in hibernate?Second level cache can be disable?First level cache can be disable and if not then why we can't? Did you ever tried?Batch processing in Hibernate.What are Derived and its usage?What is the use of bag.

Spring Interview Questions

Spring Hibernate integration Spring injection Removing setter what kind of error/exception we will get? What are the injection types? let me give deep analysis How we can achieve setter based injection and what it does? IOC and DI explain? Spring bean Life cycle ProtoType vs Single ton in spring Spring controller resolving Spring request how going to match required resources Injecting prototype in to single ton and accessing using singleton bean? Injecting singleton in to prototype and accessing using prototype bean? How to exclude specific file or class from the Component scan. How to stop providing security to specific files or URI's in Spring security. Injecting spring bean in to normal java class(non Spring bean) How can we implement interface by 2 implementation classes and how to access them?

Core Java : Coding Interview Programs

How to remove duplicate elements from ArrayList in Java? How to find if given String is palindrome in Java? How to find a missing number in a sorted array? How to calculate factorial using recursion and iteration? How do you reverse word of a sentence in Java? How to find duplicate characters from String in Java? How to reverse an integer variable in Java? How to check if a year is a leap year in Java? Write code to implement Bubble sort algorithm in Java? Write code to implement Quicksort algorithm in Java? How do you swap two integers without using temporary variable? Write a program to check if a number is power of two or not? How to reverse String in Java without using StringBuffer? Write a program to code insertion sort algorithm in Java How to solve FizzBuzz problem in Java? Write a program to print highest frequency word from a text file?

Advanced Java Interview Questions

Application server vs web server Explain the Servlet life cycle What are the implicit objects and how JSP implicit objects works or what are the usages of the JSP implicit objects? What is the difference between JBoss vs Tomact? How many ways error handling can be done in jsp? JSP include and JSP action? What is the difference between GET and POST? Dispatcher servlet functionality. How session can kill explicitly. Declaration in JSP? Statement vs Prepared statement vs Callable statement. Servlet collaboration? How exactly save points works? How to manage session? What are the various ways to keep the session? What is JDBC transaction management?

Core Java Interview Questions

How does serialization takes place in Java ? What is purpose of externalizable interface ? What are transient variables ? Why most of methods in Collections class declared as static ? What is difference between overloading and overriding ? What is hiding of members and methods - both static and instance one ? What is ThreadLocal class used for ? What is purpose of Classloader provided by Java ? What is effect of multiple classloaders on Singleton Class ? NoClassDefFoundError vs ClassNotFoundException ? What kind of exceptions we have in Java? What is run time and unchecked exceptions? How we can achieve exception free code? What we need to use among these Exception vs RuntimeException when we are creating custom exceptions? What we have to do when exception raise and when it will rise in case of checked? Hash set internal implementation? Array list how it works internally? How hash set works and what is the difference between hash map and hash set? Array list vs linked list? Comparable vs comparator…

Core Java : JVM and Architecture related Questions

How garbage collector works? Which algorithm we write to avoid memory leaks? What is strong vs weak references? What is Java Memory Model and how it keeps the String in special area? How classloader works? How java supports platform independence? Diamond dead issue? What are the basic OOPS principals? Abstract class vs interface where we use? Enum use cases where we use? Overloading vs overriding vs Method hiding? What is Marker interface and how can you implement a custom marker interface? Difference between Cohension vs Coupling?

Design Pattern Interview Questions

Singleton object? How can you achieve effective thread safety? Single ton use cases? How exactly effective single ton creates? How volatile is used in singleton ? where exactly it is needed? What is Object design pattern and when it is required? What is Abstract factory design pattern and how it is helpful?. What is Proxy design pattern and what is the use? Where we use Factory design pattern? What is Factory design pattern?

Java : ClassNotFoundException vs NoClassDefFoundError

ClassNotFoundException is an exception that occurs when you try to load a class at run time using Class.forName() or loadClass() methods and mentioned classes are not found in the classpath. NoClassDefFoundError is an error that occurs when a particular class is present at compile time, but was missing at run time. The difference from the Java API Specifications is as follows. For ClassNotFoundException:
Thrown when an application tries to load in a class through its string name using: The forName method in class Class.The findSystemClass method in class ClassLoader.The loadClass method in class ClassLoader. but no definition for the class with the specified name could be found.

For NoClassDefFoundError:
Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.
The searched-for class definition…