[Go to site: main page, start]

0% found this document useful (0 votes)
2 views12 pages

Java Coding Questions

Uploaded by

varunbotcha777
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

Java Coding Questions

Uploaded by

varunbotcha777
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Fibonacci Series

Problem

Print the first n Fibonacci numbers.

Example

Input:
n=7

Output:
0112358

Concepts Tested

 Loops

 Variables

 Recursion (follow-up)

 Dynamic Programming (advanced)

Java Solution

int n = 7;

int a = 0;
int b = 1;

[Link](a + " " + b + " ");

for (int i = 2; i < n; i++) {

int c = a + b;

[Link](c + " ");

a = b;
b = c;
}

Time Complexity

O(n)

2. Palindrome Number

A palindrome reads the same forwards and backwards.


Example

121

Reverse

121

Palindrome

Example

Input

123

Output

Not Palindrome

Java Solution

int num = 121;

int original = num;


int reverse = 0;

while (num > 0) {

int digit = num % 10;

reverse = reverse * 10 + digit;

num /= 10;
}

if (original == reverse)
[Link]("Palindrome");
else
[Link]("Not Palindrome");

Complexity

O(log n)

3. Armstrong Number
Example

153

1³ + 5³ + 3³

153

Output

Armstrong

Java

int num = 153;

int original = num;

int sum = 0;

while (num > 0) {

int digit = num % 10;

sum += digit * digit * digit;

num /= 10;
}

if (sum == original)
[Link]("Armstrong");
else
[Link]("Not Armstrong");

Complexity

O(log n)

4. Reverse a String

Example

Input

hello
Output

olleh

Method 1

String str = "hello";

String rev = "";

for (int i = [Link]() - 1; i >= 0; i--) {

rev += [Link](i);

[Link](rev);

Complexity

O(n²)

Better approach

StringBuilder sb = new StringBuilder(str);

[Link]([Link]());

O(n)

5. Check Anagram

Two strings contain the same characters.

Example

listen

silent

Anagram

Java

char[] a = "listen".toCharArray();

char[] b = "silent".toCharArray();

[Link](a);
[Link](b);

[Link]([Link](a,b));

Complexity

O(n log n)

Follow-up

Use HashMap

O(n)

6. Prime Number

Example

Input

17

Output

Prime

Java

int n = 17;

boolean prime = true;

for(int i=2;i<=[Link](n);i++){

if(n%i==0){

prime=false;

break;
}
}

[Link](prime);

Complexity

O(√n)
7. Factorial

Example

120

Java

int fact = 1;

for(int i=1;i<=5;i++){

fact*=i;

[Link](fact);

Recursive version is another common follow-up.

8. Reverse a Number

Example

12345

54321

Java

int num = 12345;

int reverse = 0;

while(num>0){

reverse = reverse*10 + num%10;

num/=10;

}
[Link](reverse);

9. Count Vowels in a String

Example

hello

Output

Java

String s = "hello";

int count = 0;

for(char c : [Link]()){

if("aeiouAEIOU".indexOf(c)!=-1)

count++;

[Link](count);

10. Find Largest Element in an Array

Example

2 8 4 10 5

10

Java

int arr[] = {2,8,4,10,5};

int max = arr[0];

for(int num : arr){


if(num>max)

max=num;

[Link](max);

Complexity

O(n)

Other Frequently Asked Basic Coding Questions

Reverse Words in a Sentence

Input

I love Java

Output

Java love I

Remove Duplicates from String

programming

progamin

Count Digits

123456

Sum of Digits
123

Swap Two Numbers

Without third variable

a = a + b;
b = a - b;
a = a - b;

Or using XOR (less common in interviews).

Second Largest Element

15386

Missing Number

1245

Frequency of Characters

banana

a=3

n=2

b=1
Remove Spaces

Hello World

HelloWorld

Count Words

Java is awesome

Check Leap Year

2024

Leap Year

GCD (HCF)

12

18

LCM

12

18


36

Binary Search

13579

Find 7

Index = 3

Linear Search

Find 8

Not Found

Bubble Sort

54321

12345

Selection Sort

Another classic sorting algorithm where you repeatedly select the minimum element
and place it in its correct position.

Insertion Sort

Builds the sorted array one element at a time by inserting each element into its proper
position.

Merge Two Sorted Arrays


135

246

123456

Move Zeros to End

10204

12400

Check if Array is Sorted

1234

Yes

You might also like