Wednesday 22 August 2018

Some Coding interview question sasked in Apple interview

1. Find if a string is an anagram of another


public class anagram {

public static void main(String[] args) {
// TODO Auto-generated method stub

boolean isAnagram=false;
String str1="armys";
String str2="marsy";
char[] c1= str1.toCharArray();
char[] c2= str2.toCharArray();
if(str1.length()!= str2.length()){
isAnagram=false;
}else{
Arrays.sort(c1);
Arrays.sort(c2);
for(int i=0;i<c1.length;i++){
if(c1[i] ==c2[i]){
isAnagram=true;
}
}
}
if(isAnagram){
System.out.println("Is anagram");
}else{
System.out.println("Not anagram");
}
}

}







2. Write a program that finds out 2 numbers adds up to a value java


public class sumOf {

public static void main(String[] args) {
// TODO Auto-generated method stub
int n=5;
int[] arr= new int[n];
for (int i=1; i<=n;i++){
arr[i-1]=i;
}
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr.length;j++ ){
int sum=arr[i]+arr[j];
if(sum == n){
System.out.println("Numbers are "+ arr[i] + " " + arr[j]);
}
}
}

}

}

5 comments:

  1. hi, nice information is given in this blog. Thanks for sharing this type of information, it is so useful for me. nice work keep it up. best sap simple finance online training institute in hyderabad

    ReplyDelete