Palindrome check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.app.interviews; | |
public class PalindromeCheck { | |
public static void main(String[] args) { | |
int number = 121; | |
int sum = 0; | |
int r; | |
int temp = number; | |
while (number != 0) { | |
r = number % 10; | |
sum = (sum * 10) + r; | |
number = number / 10; | |
} | |
if (temp == sum) | |
System.out.println("Palindrome"); | |
else | |
System.out.println("Not a palindrome"); | |
} | |
} |
Reverse a number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.app.interviews; | |
public class PalindromeCheck { | |
public static void main(String[] args) { | |
int number = 121; | |
int sum = 0; | |
int r; | |
int temp = number; | |
while (number != 0) { | |
r = number % 10; | |
sum = (sum * 10) + r; | |
number = number / 10; | |
} | |
if (temp == sum) | |
System.out.println("Palindrome"); | |
else | |
System.out.println("Not a palindrome"); | |
} | |
} |
Copy an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package interviews; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
public class CopyArray { | |
public static void main(String args[]) { | |
int[] a1 = {1, 2, 3}; | |
int[] a2 = {4, 5, 6}; | |
int[] a3 = new int[a1.length + a2.length]; | |
System.arraycopy(a1, 0, a3, 0, a1.length); | |
System.arraycopy(a2, 0, a3, a1.length, a2.length); | |
System.out.println(Arrays.toString(a3)); | |
String[] s1 = {"aa", "bb"}; | |
String[] s2 = {"cc", "ee"}; | |
List<String> s3 = new ArrayList<>(s1.length + s2.length); | |
Collections.addAll(s3, s1); | |
Collections.addAll(s3, s2); | |
System.out.println(s3); | |
} | |
} |
Check the longest word
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.app.interviews; | |
public class LongestWord { | |
public static void main(String[] args) { | |
String s = "Hi, May I know your name please? "; | |
String[] array = s.split(" "); | |
String rts = ""; | |
for (int i = 0; i < array.length; i++) { | |
if (array[i].length() > rts.length()) { | |
rts = array[i]; | |
} | |
} | |
System.out.println(rts); | |
// Using foreach | |
for (String word : array) { | |
if (word.length() > rts.length()) { | |
rts = word; | |
} | |
} | |
System.out.println(rts); | |
} | |
} |
Swap numbers of two integers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.app.interviews; | |
public class SwapNumber { | |
public static void main(String args[]) { | |
int x = 10; | |
int y = 20; | |
x = x + y; | |
y = x - y; | |
x = x - y; | |
System.out.println(x); | |
System.out.println(y); | |
} | |
} |
Common elements of two arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.app.interviews; | |
public class CommonElements { | |
public static void main(String args[]) { | |
int[] a = {1, 2, 3, 4, 5}; | |
int[] b = {2, 3, 4, 5, 6, 7, 8}; | |
for (int i = 0; i < a.length; i++) { | |
for (int j = 0; j < b.length; j++) { | |
if (a[i] == b[j]) { | |
System.out.println(a[i]); | |
} | |
} | |
} | |
} | |
} |
Top comments (0)