Javatpoint Logo
Javatpoint Logo

Microsoft Java Interview Questions

Microsoft Java Interview Questions

For Java interviews, Microsoft asks questions about the data structures, algorithms, oops concepts, etc. The interviewer never asks questions related to Java only. Interviewer can also ask questions from Array, LinkedList, Stack, Queue, String, Patterns, and Binary tree etc. The interview can also ask questions from software designing and testing.

For the Java Microsoft interview, we should also have proper knowledge of the project on which we worked before. You should have knowledge of the flow of the code, how the database is connected with the project, table structure, primary key, foreign key, etc. The top 30 Microsoft Java interview questions are given below:

1) How can we check whether the Binary Tree is BST or not?

In order to check whether the Binary Tree is BST or not, we simply check whether the left child node is smaller than the current node or not. We also check whether the right child node is greater than the current node or not. For a BST Binary tree, the left child should have smaller, and the right child should have greater value from its parent note.


2) How to find an element in a sorted array of infinite size?

In order to find an element from an infinite sorted array, we set the index at position 100. If the element which we need to found is less from the index, perform the binary search for the last 100 items. Else we set the index at position 200. We set the index by 100 until the item is greater.


3) Write a program to find the longest substring without repeating characters.

LongestSubString.java

Output:

Microsoft Java Interview Questions

4) How can we reverse a linked list of size n?

Each element of a linked list stores the address of its next element or node. In order to reverse a linked list, we need to store the address of the previous node in each node of the linked list. We can reverse the linked list by using recursion or by using iteration. We use the following steps to reverse a linked list:

  1. We first store the head.next in a temporary pointer ptr.
  2. After that, we call the reverseLL(head.next)method.
  3. We store the pointer returned by the reverseLL(head.next)in a temporary variable temp.
  4. We set the next = head( ptr points to the last node of the reversed list in temp ).
  5. The tempvariable points to the first node of the reversed linked list.

5) How can we remove duplicates from a string?

There are various ways through which we can remove duplicates from a string. We can do it by simply adding only distinct characters in the character array, by using BST, by using sorting, by using hashing, by using the unordered_map STL method, or using indexOf() method. Below is a simple example of removing duplicates from a string.

RemoveDuplicates.java

Output:

Microsoft Java Interview Questions

6) How can we merge two unsorted arrays?

There are two ways through which we can merge two unsorted arrays in a sorted manner. In the first way, we simply concatenate both the arrays and then sort them by using any sorting technique. In a second way, we sort both the arrays and then we merge them. Let's understand the first way of merging two unsorted arrays:

MergeUnsortedArrays.java

Output:

Microsoft Java Interview Questions

7) We have two linked lists, which basically represent two big numbers. Write a function that finds the sum of both the numbers and store it into the third list.


8) Write an algorithm to build a tree from given Inorder and Preorder traversals.

  1. We first take an element from the Preorder traversal and increment the index variable for picking the next element in the next recursive call.
  2. From the picked element, we create a new tree node N.
  3. Now, we get the index of that picked element from the given Inorder and store it in variable pos.
  4. We call the constructTree() method for all the elements that are available before pos and create a tree as a left subtree of node N.
  5. We call the constructTree() method for all the elements that are available after pos and create a tree as a right subtree of node N.
  6. At last, we return node N.

9) Write logic for finding the length of the longest string from a string that comprises consecutive characters.

FinalConsSubString.java

Output:

Microsoft Java Interview Questions

10) There is an array having a duplicate for each element in the array except for two elements. Write logic find these two elements.

We can easily find these two elements from the array by iterating it using for loop and checking whether element E is presented in the remaining elements of the array or not. If the element is not found, we simply store it in another array and display it on the screen.

Let's implement the logic for the above theory.

FindUniqueElements.java

Output:

Microsoft Java Interview Questions

11) Write logic to find common elements from three arrays.

FindCommonElements.java

Output:

Microsoft Java Interview Questions

12) Like data types, can we typecast an object? Explain.

Yes, we can typecast and object in Java. There are basically two ways to typecast an object, i.e., Upcasting and Downcasting.

Upcasting is a type of object typecasting in which a child class object is typecast into a parent class object. It is also known as Generalization or Widening.

Syntax:

Downcasting is another type of object typecasting in which a parent class reference object is assigned to the child class object. In Java, Downcasting is possible for limited scenarios.

Syntax:

To learn more about object typecasting, click here.


13) How can we determine whether a linked list contains a loop or cycle or not?

We use two pointers, i.e., fast and slow at the time of iterating over the linked list. The slow and fast pointers move two and one nodes in each iteration, respectively. If the linked list contains a cycle, both pointers will meet at the same point during iteration.

If both the pointers point to null, the linked list doesn't contain any loop or cycle in it.


14) What is double-checked locking in Singleton?

Double-checked locking of Singleton is a way to ensure that only a single instance of a Singleton class is created through an application life cycle. The double-checked locking means that the code checks for an existing instance of Singleton class twice with and without locking to double ensure that no more than one instance of Singleton gets created.


15) What are the scenarios in which we use the transient variable in Java?

A transient variable is a special type of variable in Java that is initialized during de-serialization with its default value. At the time of Serialization, the value of the transient variable is not serialized.

In order to prevent any object from being serialized, we use the transient variable. We can easily create a transient variable by using the transient keyword.


16) Explain volatile in Java.

In Java, volatile is a keyword that is intended to address variable visibility problems. It is another way of making a class thread-safe. The thread-safe means that multiple threads can use a method or a class instance without any problem.


17) Can we override the private method in Java?

We cannot override the private methods because we cannot access the private methods in the same way as non-private methods. The method overriding is possible in the child class only, and we cannot access the private methods in the child class.


18) Explain the difference between HashTable and HashMap in Java.

S.No. HashTable HashMap
1. It is not synchronized. It is synchronized.
2. It is thread-safe. It is not thread-safe.
3. It doesn't contain any null key or value. It contains a single null key and multiple null values.
4. It is slow. It is very fast in comparison to Hashtable.
5. It inherits Dictionary class. It inherits AbstractMap class.

19) Give the name of any two methods which we can override for an Object to be used as the key in HashMap.

The equals() and the hashcode() are two methods that we can override for an object to be used as the key in HashMap.


20) Differentiate LinkedList and ArrayList in Java.

S.No. LinkedList ArrayList
1. LinkedList internally uses doubly linked list for storing elements. ArrayList internally uses a dynamic array for storing the elements.
2. Manipulation with LinkedList is faster because of using doubly linked list. Manipulation with ArrayList is slow because of using array internally.
3. It is faster in comparison to the ArrayList. It implements the List interface only, so it acts as a list.
4. It is good for manipulation of data. It is efficient to store and access data.




You may also like:


Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA