How to implement a queue using two stacks. In this tutorial, We are going to discuss an algorithm and java code to implement a queue using two stacks.

### Categories

### Interview Questions

Technology Blog Where You Find Programming Tips and Tricks

Write a java program to find common elements in three sorted arrays. Given three sorted arrays, write a code to print intersection of three sorted arrays.

For example –

arr1 = {1, 5, 10, 20, 40, 80};

arr2 = {6, 7, 20, 80, 100};

arr3 = {3, 4, 15, 20, 30, 70, 80, 120};

**Output** : {20, 80}

20 and 80 is the common element between three sorted arrays.

Write a java code to check for balanced parentheses using stack. Given an expression containing characters ‘{‘,’}’,'(‘,’)’,'[‘,’]’. We have to write a java code to check whether an input string has valid parentheses.

An input string is valid if:

Open brackets must be closed by the same type of brackets.

Open brackets must be closed in the correct order.

Example –

i) {[]}) – Invalid

ii) {()}[] – Valid

Write a java program to reverse a string using stack data structure. Given an input string, We have to write a java code to reverse a string using stack.

In my previous tutorial, I have explained java program to reverse a string using recursion.

Before solving this problem, Let’s understand what is a stack data structure.

Java code to find pairs with given sum in a sorted array. Given an array A of size N. Write a java code to find all pairs in the array that sum to a number equal to K. If no such pair exists then output will be –1. Array elements are distinct and in a sorted order.

For example –

**Input** :

arr[] = {1, 2, 3, 4, 5, 6, 7};

sum = 9

**Output**:

Pairs with given sum 9 is

Pair (2 , 7 )

Pair (3 , 6 )

Pair (4 , 5 )

Given a sorted array which contains duplicate elements, we have to write a java code to find first and last position of a number in a sorted array. Given a number x, write a code to find it’s first and last index in an array.

For example –

Suppose the input array is arr[] : **{1, 4, 7, 8, 8, 11, 11, 11, 11, 12, 13, 13}** and number to searched is **11** then the first index of 11 is 5 and last index is 8.

Given **two sorted arrays**, Write a java code to find intersection of two arrays.

For example, if the input arrays are:

arr1[] = {2, 3, 6, 7, 9, 11}

arr2[] = {4, 6, 8, 9, 12}

Then your program should print intersection as {6, 9}. Before writing actual code, let’s first discuss different approaches to solve this problem.

Write a java program to move all zeroes to end of array. Given an array of random integers move all zeroes to end of array. The order of all other elements of an array should be same.

In this tutorial, We are going to discuss and write a java program to move zeroes to end of an array. It is another interesting problem which generally asked in a technical interviews.

For example –

**Input** : {1, 2, 0, 4, 0, 5, 3, 8}

**Output**: {1, 2, 4, 5, 3, 8, 0, 0}

You can see in the example, we have moved zeroes to the end of an array.

Write a java program to find sum of digits of a number. Given an input number, we have to write a java code to calculate sum of digits of a number.

For example –

i)

Input : 256

Output : 13 (2+5+60

ii)

Input : 15

Output : 6 (1+5)