What is Hashmap data structure? What is the need of Hashmap? How Hashmap data structure API works in Java? What is Hashcode? Can 2 objects have same hashcode?

What is Hashmap data structure?
What is the need of Hashmap?
How
Hashmap data structure API works in Java?
What is Hashcode?
Can 2 objects have same hashcode?


This is the famous interview question for the beginners as well as for experienced, So Let's see what it is all about. Hashmap is very popular data structure and found useful for solving many problems due to O(1) time complexity for both get and put operation.

What is the need of Hashmap data structure? What problem it solves?


When we have many objects and if we want to search a particular object among them, 
How to search??
(Say we have 100 Student object and we want to search Student having Roll No. 35, how to do?)

We pick one object and see, this is the object we are looking for? No. 

Pick next object and check again. Repeat the steps until we found the object.
Imagine the time it will take???


The time it will take to search a particular object will be even high if total objects are more.
This is where hashmap will come in picture.
Hashmap arranges the objects in such a way, that searching any object in hashmap will be done in almost O(1) complexity.
How arranging items in particular way can make searching faster. Lets see with example,

Ms. Khyati is working for XYZ Computers in Bangalore and for work assignment she has to relocate to Pune for 1 month.
Khyati noted down important house items to carry for 1 month and started packing like shown below,

She packed all items randomly in 2 boxes.


How PreparedStatement in Java prevents SQL Injection?

What is PreparedStatement in java?
Why use PreparedStatement in Java JDBC?
How prepared statement prevents SQL Injection?
Difference between Statement and PreparedStatement in Java?


This is the famous interview question for the beginners, So Let's see what it is all about.

SQL Injection is code injection technique where SQL is injected by user (as part of user input) into the back end query, and ultimately changes query purpose which upon execution gives harmful result.

Detailed explanation on SQL Injection: What is SQL Injection?


How can SQL Injection happen.


At server side, queries generally by themselves are not complete and require user data to make it complete, meaningful and executable.
"select * from user where username = ' " + username + " ' ";
Above query is not complete as it has dependency on username variable.
Now if username variable is filled by third party, then there are chances that user data contains SQL,

Take an example. Application is asking user to enter user name,
Enter user name:________________________

Enter user name:___jayesh'; delete from user where id='1__

At Server Side,

username = "jayesh'; delete from user where id='1"
Final Query = "select * from user where username = ' jayesh'; delete from user where id='1 ' ";

If you observe final query, upon execution it will delete the record from user table which was never the purpose of original query and this is called SQL Injection attack. 

Because of user data (which can be anything and uncontrolled) involvement in formation of query, SQL Injection attack can happen.

Detailed explanation on: How can SQL Injection happen?

How PreparedStatement in Java prevents SQL Injection?


To understand this, Lets see steps involved in query execution.
1. Compilation Phase.

2. Execution Phase.

Whenever SQL server engine receives a query, It has to pass through below phases,

What is SQL Injection?

Explain SQL Injection along with example?


This is the famous interview question for the beginners, So Let's see what it is all about.

SQL Injection is code injection technique where SQL is injected by user (as part of user input) into the back end query. Injected SQL data alters the purpose of original query and upon execution can gives harmful result. A SQL injection attack is very dangerous and attacker can,

1. Read sensitive data from the database.
2. Update database data (Insert/Update/Delete).

3. Slowdown the complete Database system etc.


Boundary Traversal of Binary Tree.

Boundary Traversal of binary tree OR
Print Circumference of Binary Tree OR
Given a Binary Tree, Print its Perimeter OR
Print outside nodes of Binary Tree OR
Print Edge nodes of Binary Tree OR

Print Boundary of Binary Tree.


We have to print the boundary nodes of given Binary Tree in anti-clockwise starting from the root.


Let us first understand what we want to achieve? what is the input and what will be the expected output?



Zig Zag Traversal of Binary Tree.

Level order traversal in spiral form OR
Printing a Binary Tree in Zig Zag Level-Order OR
Print a binary tree in Zig Zag way.


Given a binary tree, write a program to print nodes of the tree in spiral order. You can also say it as Spiral order traversal of a tree. Let us first understand what we want to achieve? what is the input and what will be the expected output?
 

N Queens puzzle Optimized.


The N Queen is the problem of placing N Chess queens on an N×N chessboard so that no two queens attack each other.



Let us first understand what we want to achieve? what is the input and what will be the expected output?

You are given a chess board of N * N size, you have to place N Queens in chess board in such a way that no queens are attacking each other.

Sample Input and output for 4 * 4 chess board



Remember, Queens attacks on same row, on same column as well as diagonally.

N Queens Puzzle.


The N Queen is the problem of placing N Chess queens on an N×N chessboard so that no two queens attack each other.



Let us first understand what we want to achieve? what is the input and what will be the expected output?

You are given a chess board of N * N size, you have to place N Queens in chess board in such a way that no queens are attacking each other.

Sample Input and output for 4 * 4 chess board


Remember, Queens attacks on same row, on same column as well as diagonally.

Find the element that appears once others appears THRICE.


Find the element that appears once others appears THRICE.



Given an array of integers. All numbers occur thrice except one number which occurs once. Find the number in O(n) time & constant extra space.

Let us first understand what we want to achieve? what is the input and what will be the expected output?

Question:
       Input:     {17, 10, 17, 2, 17, 10, 10, 4, 4, 4}
       Output:   2

       Input:     {17}
       Output:  17

       Input:     {17, 17, 17, 2}
       Output:   2

Algorithm


This problem can be solved in multiple ways but solution might not fulfill time and space complexity constraints given in problem statement.
Time complexity to solve this problem should not be greater then O(n)
Space complexity to solve this problem should not be greater then O(1)
Bit Manipulation will be helpful in this situation.

Before proceeding further lets just see XOR operation and facts.



Find the element that appears once others appears TWICE.


Find the element that appears once others appears TWICE.



Given an array of integers. All numbers occur twice except one number which occurs once. Find the number in O(n) time & constant extra space.

Let us first understand what we want to achieve? what is the input and what will be the expected output?

Question:
       Input:     {2, 3, 5, 4, 5, 3, 4}
       Output:   2

       Input:     {2}
       Output:   2

       Input:     {2,1,2}
       Output:   1

Algorithm


This problem can be solved in multiple ways but solution might not fulfill time and space complexity constraints given in problem statement.
Time complexity to solve this problem should not be greater then O(n)
Space complexity to solve this problem should not be greater then O(1)
Bit Manipulation will be helpful in this situation.

Before proceeding further lets just see XOR operation and facts.



Construct a Binary Tree from In-order and Pre-order traversals.

How to construct a Binary Tree from given In order and Pre order traversals.


Let us first understand what we want to achieve? what is the input and what will be the expected output?

Question:
Two traversals are given as input,
  int inorder[] =  {20, 30, 35, 40, 45, 50, 55, 60, 70};
  int preorder[] = {50, 40, 30, 20, 35, 45, 60, 55, 70};
By using above 2 given In order and Pre Order traversal, construct Binary Tree like shown below,