# Top 100+ Java coding interview questions – Java2Blog

### question 22 : Given an range check zeroes, ones and twos only. Write a function to sort the given array in O ( newton ) clock complexity .

For example:

 1 2 3 4 5 6 7 Input : [ 1 , 2 , 2 , 0 , 0 , 1 , 2 , 2 , 1 ] Output : [ 0 , 0 , 1 , 1 , 1 , 2 , 2 , 2 , 2 ]

Solution : Sort an array of 0s, 1s and 2s.

### question 23 : Find local minimum in array

A local minimum is less than its neighbours For example:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Input : int [ ] arr = { 10 , 5 , 3 , 6 , 13 , 16 , 7 } ; Output : 2 int [ ] arr = { 11 , 12 , 13 , 14 } ; Output : 11 int [ ] arr = { 10 } ; Output : 10 int [ ] arr = { 8 , 6 } ; Output : 6

### question 24 : Sliding window utmost in java

Given an Array of integers and an Integer thousand, Find the maximal element of from all the conterminous subarrays of size K. For example:

 1 2 3 4 5 6 Input : Input : int [ ] arr = { 2 , 6 , – 1 , 2 , 4 , 1 , – 6 , 5 } int k = 3 output : 6 , 6 , 4 , 4 , 4 , 5

Solution : Find the local minima in array.

### interrogate 25 : Count phone number of occurrences ( or frequency ) of each chemical element in a sorted array

Given a grouped array of integers containing duplicates. Find the frequency of every singular element present in the array.
Frequency is defined as the total of happening of any component in the array. For example :

 1 2 3 4 5 6 7 8 9 10 11 Input : Input : int [ ] arr = { 1 , 1 , 1 , 3 , 3 , 4 , 5 , 5 , 6 , 6 } ; Output : Frequency of 1 is : 3 Frequency of 3 is : 2 Frequency of 4 is : 1 Frequency of 5 is : 2 Frequency of 6 is : 2

Solution : Count number of occurrences (or frequency) of each element in a sorted array.

### question 26 : Find subarrays with given sum in an range .

Given an Array of not negative Integers and a numeral. You need to print all the start and ending indices of Subarrays having their sum equal to the given integer.
For example :

 1 2 3 4 5 6 7 8 9 Input : Input – int [ ] arr = { 2 , 3 , 6 , 4 , 9 , 0 , 11 } ; int num = 9 Output – starting index : 1 , Ending index : 2 starting index : 5 , Ending index : 5 starting index : 5 , Ending index : 6

Solution : Find subarrays with given sum in an array.

### question 27 : Find vertex element in the array .

Peak Element is the element of the array which is GREATER THAN / EQUAL TO its neighbours, that is, for an component at i thursday index, the neighbor elements at index i-1 & i+1 must be greater than equal to element at i thorium position .Solution : Find peak element in the array.

### wonder 28 : discovery leaders in an align .

We need to print all the leaders present in the array. chemical element is the leader if it is greater than right side of elements.

 1 2 3 4 arr [ ] = { 14 , 12 , 70 , 15 , 99 , 65 , 21 , 90 } Here 99 and 90 are leader elements

For example:Solution : Find leaders in an array.

### question 29 : count 1 ’ south in grouped Binary Array .

Print number of 1 ’ mho in a given screen Binary Array.
For example :

 1 2 3 4 5 6 7 Input : int [ ] arr = { 0 , 0 , 0 , 1 , 1 , 1 , 1 } ; output : 4 int [ ] arr = { 0 , 0 , 1 } ; output : 1

### question 30 : Find first repeating element in an align of integers .

Find the first repeating element in array of integers.
For example :

 1 2 3 4 5 Input : Input : array [ ] = { 10 , 7 , 8 , 1 , 8 , 7 , 6 } Output : 7 [ 7 is the first element actually repeats ]

Solution : Find first repeating element in an array of integers.

### interview 31 : check if array Elements are Consecutive .

Given an array, we need to check if align contains consecutive elements.
For example :

 1 2 3 4 5 6 7 8 9 10 11 Input : array [ ] = { 5 , 3 , 4 Read more: Top 4 for interview questions in 2022 , 1 , 2 } Output : true As array contains consecutive elements from 1 to 5 Input : array [ ] = { 47 , 43 , 45 , 44 , 46 } Output : true As array contains consecutive elements from 43 to 47 Input : array [ ] = { 6 , 7 , 5 , 6 } Output : false As array does not contain consecutive elements .

Solution : Check if Array Elements are Consecutive.

### question 32 : Permutations of align in java .

Given array of distinct integers, print all permutations of the array.
For example :

 1 2 3 4 5 6 7 8 9 10 11 12 array : [ 10 , 20 , 30 ] Permuations are : [ 10 , 20 , 30 ] [ 10 , 30 , 20 ] [ 20 , 10 , 30 ] [ 20 , 30 , 10 ] [ 30 , 10 , 20 ] [ 30 , 20 , 10 ]

Solution : Permutations of array in java.

### interrogate 33 : Rotate an array by K positions .

For example :

 1 2 3 4 5 N = 6 and k = 2 If Arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } and k = 2 then rotated array will be    { 5 , 6 , 1 , 2 ,    3 ,    4 }

Solution : Rotate an array by K positions.

### question 34 : Stock Buy Sell to Maximize net income .

Given an array of integers representing stock price on single day, find soap profit that can be earned by 1 transaction.
So you need to find pair ( buyDay, sellDay ) where buyDay < = sellDay and it should maximise the profit.
For example :

 1 2 3 4 5 int arr [ ] = { 14 , 12 , 70 , 15 , 99 , 65 , 21 , 90 } ; Max profit can be gain by buying at 1th day ( 0 based indexing ) and sell at 4th day . Max profit = 99 – 12 = 87

Solution : Stock Buy Sell to Maximize Profit.

### question 35 : Find maximum deviation between two elements such that larger component appears after the smaller total .

Given array of integers, find maximum deviation between two elements such that larger element appears after the smaller number
For example :

 1 2 3 4 int arr [ ] = { 14 , 12 , 70 , 15 , 95 , 65 , 22 , 30 } ; Max Difference = 95 – 12 = 83

Solution : Maximum difference between two elements such that larger element appears after the smaller number.

### interrogate 36 : search in a rowing wise and column wise sorted matrix .

Given course knowing and column wise sorted matrix, we need to search component with minimum time complexity .Solution : Search in a row wise and column wise sorted matrix.

### interrogate 37 : Largest sum contiguous subarray .

Largest total conterminous subarray is the undertaking of finding the contiguous subarray within a linear align of numbers which has the largest sum.
For example :

 1 2 3 for the sequence of values − 2 , 1 , − 3 , 4 , − 1 , 2 , 1 , − 5 , 4 ; the contiguous subarray with the largest sum is 4 , − 1 , 2 , 1 , with sum 6

Solution : Largest sum contiguous subarray.

### question 38 : Find the conterminous Subarray with Sum to a Given Value in an align .

Given an array of positive integer and given value X, find contiguous substitute array whose sum is equal to X.
For example :

 1 2 3 4 5 6 arr [ ] = { 14 , 12 , 70 , 15 , 99 , 65 , 21 , 90 } ; X = 97. Sum found between index 1 to 3 Elements are 12 , 17 and 15

Solution : Find the Contiguous Subarray with Sum to a Given Value in an array.

### interview 39 : Longest Common Prefix in an array of Strings in java .

Given an array of positive integer and given value X, find contiguous substitute array whose total is equal to X.
For example :

 1 2 3 4 String [ ] strArr = { “java2blog” , “javaworld” , “javabean” , “javatemp” } ; So Longest common prefix in above String array will be “ java ” as all above string starts with “ java ” .

Solution : Longest Common Prefix in an array of Strings in java.

### motion 40 : Find all subsets of set ( ability set ) in coffee .

Given a bent of distinct integers, arr, return all possible subsets ( the power set ).
For example :

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Input : nums = [ 1 , 2 , 3 ] Output : [ [ 3 ] , [ 1 ] , [ 2 ] , [ 1 , 2 , 3 ] , [ 1 , 3 ] , [ 2 , 3 ] , [ 1 , 2 ] , [ ] ]

Solution : Find all subsets of set in java.

## Stack

Solution : Insert node in binary search tree

### doubt 70 : can you write algorithm to delete a lymph node in binary search tree .

Solution : Delete node in binary search tree

### doubt 71 : How can you find minimum and maximal elements in binary search tree ?

Solution : leftmost and rightmost nodes of binary search tree are minimum and maximum nodes respectively

### question 72 : How to find humble common ancestor ( LCA ) in binary star search tree .

You need to write a program to find LCA in binary star search tree. Solution : broadcast to find LCA in binary star search tree.

### Question 77 : Write an algorithm to implement bubble sort?

Solution : Bubble sort in java

### Question 78 : Write an algorithm to implement insertion sort sort?

Solution : Insertion sort in java

### Question 79 : Write an algorithm to implement selection sort sort?

Solution : Selection sort in java

### wonder 80 : toilet you write algorithm for unify sort and besides do you know complexity of unite classify ?

Solution : Merge sort in java

### Question 81 : Do you know how to implement Heap sort?

Solution : implement Heap sort in java

### question 82 : implement quick sort in java ?

Solution : implement Quick sort in java

### doubt 83 : enforce husk kind in coffee ?

Solution : implement Shell sort in java

### question 84 : enforce Counting screen in java ?

Solution : implement Counting sort in java

### Question 85 : What is binary search? Can you write an algorithm to find an element in sorted array using binary search?

Solution :Binary search algorithm in java

generator : https://gauday.com
Category : interview

We will be happy to hear your thoughts Enable registration in settings - general