Java Interveiw Coding
Java Interveiw Coding
Write a return method that check if a string is build out of the same
letters as another string.
Ex: same("abc", "cab"); -> true , same("abc", "abb"); -> false:
Solution 1:
public static boolean same(String a, String b) {
char[] ch1 = a.toCharArray(),
char[] ch2 = b.toCharArray();
Arrays.sort(myar1);
Arrays.sort(myar2);
String a1="", a2="";
for(char each: ch1)
a1 +=each;
return a1.equals(a2) ;
}
Solution 2:
public static boolean Same(String str1, String str2) {
str1 = new TreeSet<String>(Arrays.asList( str1.split("") ) ).toString( );
str2 = new TreeSet<String>(Arrays.asList( str2.split("") ) ).toString( );
return str1.equals(str2);
2. Write a return method that can remove the duplicated values
from String
Ex: removeDup("AAABBBCCC") ==> ABC
Solution 1:
public static String removeDup( String str) {
String result = "";
for (int i = 0; i < str.length(); i++)
if (!result.contains("" + str.charAt(i)))
result += "" + str.charAt(i);
return result;
}
Solution 2:
public static String removeDup(String str) {
str = new LinkedHashSet<String>(Arrays.asList(str.split(""))).toString();
str = str.replace(", ","").replace("[","").replace("]","");
return str;
}
3. Write a return method that can find the unique characters from the
String
Ex: unique("AAABBBCCCDEF") ==> "DEF";
Solution 1:
public static String unique(String str) {
String[ ] arr=str.split("");
String unique1="";
for(int j=0; j<arr.length; j++) {
int num=0;
for(int i=0; i<arr.length; i++ ) {
if(arr[i].equals(arr[j]) )
num++;
}
if(num==1)
unique1+=arr[j];
}
return unique1;
}
Solution 2:
public static String Unique(String str) {
String result ="";
for(String each : str.split(""))
result += ( (Collections.frequency(Arrays.asList(str.split("")), each)) ==1 ) ? each : "";
return result;
}
4. Write a return method that can reverse String
Ex: Reverse("ABCD"); ==> DCBA
Solution 1
public static String StrReverse(String str) {
String reverse="";
for(int i=str.length()-1; i >= 0; i--)
reverse += str.toCharArray()[i];
return reverse;
}
Solution 2
public static String Reverse(String str) {
return new StringBuffer(str).reverse().toString());
}
5. Write a return method that can find the frequency of
characters
Ex: FrequencyOfChars("AAABBCDD") ==> A3B2C1D2
solution 1:
public static String FrequencyOfChars(String str) {
String nonDup = "";
for(int i=0; i < str.length(); i++)
if(!nonDup.contains(""+str.charAt(i)))
nonDup+= ""+str.charAt(i);
result+=b.substring(j, j+1)+count;
}
return result;
}
Solutions 4:
public static String frequency(String str) {
String nonDup="", result="";
return result;
}
6. Write a return method that can verify if a password is valid or not.
requirements:
1. Password MUST be at least have 6 characters and should not contain space
2. PassWord should at least contain one upper case letter
3. PassWord should at least contain one lowercase letter
4. Password should at least contain one special characters
5. Password should at least contain a digit
if all requirements above are met, the method returns true, otherwise
returns false
Solution 1:
public static boolean PassWordvalidation(String password) {
String lowercase="(.*[a-z].*)";
String uppercase="(.*[A-Z].*)";
String numbers="(.*[0-9].*)";
String specialchars="(.*[ -/, :-@].*)";
boolean HasLower = password.matches(lowercase),
HasUppere = password.matches(uppercase),
HasDigits = password.matches(numbers),
HasSpecial = password.matches(specialchars),
Valid = false;
if(password.length() >= 6 && !password.contains(" "))
if( HasLower && HasUppere && HasDigits && HasSpecial)
Valid = true;
return Valid;
}
7. Write a method that can find the maximum number from an int
Array
Solution 1:
public static int maxValue( int[ ] n ) {
int max = Integer.MIN_VALUE;
for(int each: n){
if(each > max){
max = each;
}
}
return max;
}
Solution 2:
public static int maxValue( int[ ] n ) {
Arrays.sort( n );
return n [ n.lenth-1 ];
}
8. Write a method that can find the maximum number from an int
Array
Solution 1:
public static int maxValue( int[] n ) {
int min = Integer.MAX_VALUE;
for(int each: n){
if(each < min){
min = each;
}
}
return min;
}
Solution 2:
public static int maxValue( int[] n ) {
Arrays.sort( n );
return n [ 0 ];
}
9. Write a return method that can sort an int array in Ascending
order without using the sort method of the Arrays class
Ex:
int[] arr = {10, 9, 8, 7};
arr = Sort(arr); ==>{ 7, 8, 9, 10};
Solution1 :
public static int[] Sort(int[] a) {
ArrayList<Integer> list=new ArrayList<Integer>();
for(int each: a)
list.add(each);
return min;
}
10. Write a return method that can sort an int array in descending order
without using the sort method of the Arrays class
Ex: int[] arr = {10,20,7, 8, 90};
arr = Sort(arr); ==> {90, 20, 10, 8, 7};
Solution 1:
public static int[ ] Sort(int[ ] a) {
ArrayList<Integer> list=new ArrayList<Integer>();
for(int each: a)
list.add(each);
return max;
}
11. Write a function:
that, given an integer N (1 < N < 100), returns an array containing
N unique integers that sum up to 0. The function can return any
such array. For example, given N = 4, the function could return
[1,0, -3,2] or [-2,1, -4,5]. The answer [1, -1,1,3] would be incorrect
(because value 1 occurs twice). For N = 3 one of the possible
answers is [-1,0,1] (but there are many more correct answers).
Solution:
public static int[ ] Solution(int N ){
int[ ] result = new int[N];
int sum = 0;
for(int i=0; i < N-1; i++) {
result[i] =i;
sum += i;
}
result[N-1] = -sum;
return result;
}
12. Given an array of 3 characters print all permutation
combinations from the given characters
return set;
}
13. Write a method which can identifies given number is even or
odd
EX:
identify(5) -> "Odd"
identify(6) -> "Even"
Solution:
public static String identifyOddEven( int n ) {
return n%2==0 ? "Even" : "odd" ;
}
14. Write a method that can divide two numbers without using
division operator
Solution:
public static void devides(int num1, int num2) {
if(num2==0) {
System.out.println("Invalid Number");
return;
}
System.out.print(num1 +" devid by "+num2 +" is: ");
int count =0;
while(num1 >= num2) {
num1 -= num2;
count++;
}
System.out.println(count+" and remainder is "+num1);
}
15. Write a method which prints out the numbers from 1 to 30 but
for numbers which are a multiple of 3, print "FIN" instead of the
number and for numbers which are a multiple of 5, print "RA" instead
of the number. for numbers which are a multiple of both 3 and 5, print
"FINRA" instead of the number.
Solution 1:
public static void FINRA( ) {
String result = "";
for(int i=1; i <= 30; i++) {
if(i % 5==0 && i %3 ==0)
result += "FINRA ";
else if(i%5 == 0)
result += "RA ";
else if(i%3==0)
result+="FIN ";
else
result += i+" ";
}
System.out.println(result);
}
Solution 2:
public static void FINRA() {
String result = "";
for(int i=1; i <= 30; i++) {
result += (i % 5 ==0 && i %3 ==0)? "FINRA " : (i%5 == 0) ? "RA "
:(i%3 == 0) ? "FIN " : i+" ";
}
System.out.println(result);
}
Solution 3:
public static void FINRA() {
String[ ] myarr= new String[30];
for( int i=0; i <= 29; i++ )
myarr[i] = ""+(i+1);
Solution 1:
int a = 10; int b = 20;
a = a +b;
b = a - b;
a = a - b;
Solution 2:
int a = 10; int b = 20;
a = a^b;
b = a^b;
a = a^b;
17. Write a function:
that, given a positive integer N, prints the consecutive numbers from 1 to
N, each on a separate line. However, any number divisible by 2, 3 or 5
should be replaced by the word Codility, Test or Coders respectively. If a
number is divisible by more than one of the numbers: 2,3 or 5, it should be
replaced by a concatenation of the respective words Codility, Test and
Coders in this given order. For example, numbers divisible by both 2 and 3
should be replacée by CodilityTest and numbers divisible by all
three numbers: 2,3 and 5, should be replaced by CodilityTestCoders.
Codility
Coders
CodilityTest
7
Codility
Test
Codi1ityCoders
11
CodilityTest
13
Codility
TestCoders
Codility
17
....
Solution:
public static void solution( int N ) {
String result = "";
for (int i = 1; i <= N; i++) {
if(i %2 ==0 && i%3 == 0 && i %5==0)
result += "CodilityTestCoders\n";
else if(i %2 ==0 && i%3 == 0)
result += "CodilityTest\n";
else if(i % 2==0 && i %5==0)
result += "CodilityCoders\n";
else if(i % 3 == 0 && i % 5 ==0)
result +="TestCoders\n";
else if(i % 2 ==0)
result += "Codility\n";
else if (i % 5 == 0)
result += "Coders\n";
else if (i % 3 == 0)
result += "Test\n";
else
result += i + "\n";
}
System.out.println(result);
}
18. Given a list of people' names: "Ahmed", "John", Eric",
"Ahmed".....
Write a java operation to remove all the names named Ahmed
Solution 1:
ArrayList<String> names = new ArrayList<>(Arrays.asList("Ahmed", "John", Eric",
"Ahmed".));
names.removeAll( Arrays.asList("Ahmed"));
System.out.println(names);
Solution 2:
List<String> names = new ArrayList<>(Arrays.asList("Ahmed", "John", Eric",
"Ahmed".));
for(ListIterator<String> it=names.listIterator(); it.hasNext();)
if(it.next().equals("Ahmed"))
it.remove();
System.out.println(names);
Solution 3:
List<String> names = new ArrayList<>(Arrays.asList("Ahmed", "John", Eric",
"Ahmed".));
List<String> names2 = new ArrayList<>();
names.forEach(p-> {if(p.equals("Ahmed")) names2.add(p);} );
System.out.println(names2);
Solution 4:
List<String> names = new ArrayList<>(Arrays.asList("Ahmed", "John", Eric",
"Ahmed".));
Iterator<String> it = names.iterator();
while(it.hasNext()) {
if(it.next().equals("Ahmed"))
it.remove();
}
System.out.println( names );
19. Given a list of Integers 1, 2, 3, 4, 5, 6 ....etc. remove all values
greater than 100.
Solution 1:
ArrayList<Integer> list1 = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,101,200,300));
ArrayList<Integer> list2 = new ArrayList<>();
for(int each : lists1)
if( each < 100)
lists2.add(each);
list1=list2;
System.out.println(list1);
Solution 2:
List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,101,200,300));
Iterator<Integer> it = list.iterator();
while(it.hasNext())
if(it.next()>100)
it.remove();
System.out.println(list);
Solution 3:
List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,101,200,300));
for(ListIterator<Integer> il = list.listIterator(); il.hasNext();)
if(il.next()>100)
il.remove();
System.out.println(list);
Solution 4:
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,101,200,300));
list.removeIf(p -> p>100);
System.out.println(list);
20. Given an array of 3 characters print all permutation
combinations from the given characters
Solution:
public static void printPermutation(char[] ch) {
for(String s: permutation(ch))
System.out.println(Arrays.toString( s.toCharArray( ) ) );
}
return set;
}