Interview Coding Questions
1. Palindrome of a String
public class Palindrome {
static boolean isPalindrome(String str)
{
int i = 0, j = str.length() — 1;
while (i < j) {
if (str.charAt(i) != str.charAt(j))
return false;
i++;
j — ;
}
return true;
}
public static void main(String[] args)
{
String str = “aba”;
if (isPalindrome(str))
System.out.print(“Palindrome”);
else
System.out.print(“Not a Palindrome”);
}
}
2. Palindrome of a number
public class Palindrome {
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int sum = 0, r;
int temp = n;
while(n>0)
{
r = n % 10;
sum = (sum*10)+r;
n = n/10;
}
if(temp==sum)
System.out.println(“Palindrome number”);
else
System.out.println(“Not a Palindrome number”);
}
}
3. Reverse an array
import java.util.*;
import java.util.stream.*;
public class Main
{
public static void main(String[] args) {
Integer[] intArray = {10,20,30,40,50,60,70,80,90};
//print array starting from first element
System.out.println(“Original Array:”);
for(int i=0;i<intArray.length;i++)
System.out.print(intArray[i] + “ “);
System.out.println();
//print array starting from last element
System.out.println(“Reversed Array:”);
for(int i=intArray.length-1;i>=0;i — )
System.out.print(intArray[i] + “ “);
}
}
4. Reverse given Integer
public class ReverseNumber
{
public static void main(String[] args)
{
int number = 789435, reverse = 0;
while(number != 0)
{
int remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number/10;
}
System.out.println(“The reverse number: “ + reverse);
}
}
5. Reverse of a linked list
class LinkedList {
static Node head;
static class Node {
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
Node reverse(Node node)
{
Node prev = null;
Node current = node;
Node next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
node = prev;
return node;
}
void printList(Node node)
{
while (node != null) {
System.out.print(node.data + “ “);
node = node.next;
}
}
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node(85);
list.head.next = new Node(15);
list.head.next.next = new Node(4);
list.head.next.next.next = new Node(20);
System.out.println(“Original Linked list:”);
list.printList(head);
head = list.reverse(head);
System.out.println(“Reversed linked list:”);
list.printList(head);
}
}
6. Remove duplicates from a given array
public class RemoveDuplicate{
public static int removeDuplicateElements(int arr[], int n){
if (n==0 || n==1){
return n;
}
int[] temp = new int[n];
int j = 0;
for (int i=0; i<n-1; i++){
if (arr[i] != arr[i+1]){
temp[j++] = arr[i];
}
}
temp[j++] = arr[n-1];
for (int i=0; i<j; i++){
arr[i] = temp[i];
}
return j;
}
public static void main (String[] args) {
int arr[] = {5,10,15,20,25,30,35,40,45,50};
int length = arr.length;
length = removeDuplicateElements(arr, length);
//printing array elements
for (int i=0; i<length; i++)
System.out.print(arr[i]+” “);
}
}
7. Remove duplicate characters in a String
import java.util.*;
class StringDuplicate
{
static String removeDuplicate(char str[], int n)
{
int index = 0;
for (int i = 0; i < n; i++)
{
int j;
for (j = 0; j < i; j++)
{
if (str[i] == str[j])
{
break;
}
}
if (j == i)
{
str[index++] = str[i];
}
}
return String.valueOf(Arrays.copyOf(str, index));
}
// Driver code
public static void main(String[] args)
{
char str[] = “homealone”.toCharArray();
int n = str.length;
System.out.println(removeDuplicate(str, n));
}
}
8. Find the missing number in a given integer array
class FindNum{
// Function to find missing number
static int getMissingNo(int a[], int n)
{
int x1 = a[0];
int x2 = 1;
for (int i = 1; i < n; i++)
x1 = x1 ^ a[i];
for (int i = 2; i <= n + 1; i++)
x2 = x2 ^ i;
return (x1 ^ x2);
}
public static void main(String args[])
{
int a[] = { 1, 2, 3, 4, 6 };
int miss = getMissingNo(a, 5);
System.out.println(miss);
}
}
9. Find the largest and smallest number in an unsorted integer array
public class SmallestLargestInArray {
public static void main(String[] args)
{
int inArray[] = {7,5,6,7,3,2,5,8,10,88};
int arraySize = inArray.length;
findSmallestlargestElement(inArray, arraySize);
}
private static void findSmallestlargestElement(int[] inArray, int arraySize) {
int min = inArray[0];
int max = inArray[0];
//Traverse through the array
for (int i=0; i< arraySize; i++)
{
if (inArray[i] > max)
{
max = inArray[i];
}
if (inArray[i] < min)
{
min = inArray[i];
}
}
//print smallest and largest numbers in the input array
System.out.println(“Minimum Number: “ + min);
System.out.println(“Maximum Number: “ + max);
}
}
10. Add two numbers without using the plus operator
import java.io.*;
class AddTwo
{
static int Add(int x, int y)
{
// Iterate till there is no carry
while (y != 0)
{
int carry = x & y;
x = x ^ y;
y = carry << 1;
}
return x;
}
// Driver code
public static void main(String arg[])
{
System.out.println(Add(89, 56));
}
}
11. Find all pairs of integer array whose sum is equal to a given number
public class FindEqu
{
public static void main(String args[])
{
int[] arr = { 1, 5, 7, -1, 5 };
int sum = 6;
getPairsCount(arr, sum);
}
public static void getPairsCount(int[] arr, int sum)
{
int count = 0;
for (int i = 0; i < arr.length; i++)
for (int j = i + 1; j < arr.length; j++)
if ((arr[i] + arr[j]) == sum)
count++;
System.out.printf(“Number of pairs is %d”,count);
}
}
12. Swap two numbers without using the third variable
import java.*;
class SwapTwo {
public static void main(String a[])
{
int x = 9;
int y = 5;
x = x + y;
y = x — y;
x = x — y;
System.out.println(“After swaping:”+ “ x = “ + x + “, y = “ + y);
}
}