GK
BAN USERpublic static void main(String[] args) {
String s1 = "amazon", s2 = "azonam";
boolean matchFound = false;
for(int i=0; i<s2.length(); i++) {
for(int j=0; j<s2.length(); j++) {
if(i!=j) {
String strTemp = "" + s2.charAt(i) + s2.charAt(j) + collectRestOfWords(s2, i, j);
if(strTemp.equals(s1)) {
System.out.println("Word " + strTemp + " matched!");
matchFound = true;
break;
}
}
}
if(matchFound) {
return;
}
}
System.out.println("No match found");
}
public static void main(String[] args) {
int[] a = {1, 2,1,4,5,-10};
int count = 0, maxCount = 0;
int startInd = 0, endInd = 0;
for(int i=0; i<a.length; i++) {
if(a[i]>0) {
count++;
} else {
if(maxCount<count) {
startInd = i - count;
endInd = i-1;
maxCount = count;
count = 0;
}
}
}
if(maxCount<count) {
startInd = a.length - count;
endInd = a.length-1;
maxCount = count;
}
System.out.println(maxCount);
System.out.println(startInd);
System.out.println(endInd);
}
public static Integer missingNumber(int[] a, int startNum) {
Arrays.sort(a);
int tempNum = startNum;
for(int i=0; i<a.length; i++) {
if(a[i]!=tempNum) {
break;
} else {
tempNum++;
}
}
return tempNum;
}
public static void main(String[] args) {
int[] a = {1,2,3,5,6,7,8,9,10};
System.out.println("Missing Number is: " + missingNumber(a, 1));
}
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5, 5, 5, 6, 7, 7, 8};
LinkedHashMap<Integer, Integer> lhm = new LinkedHashMap<>();
for(int i:a) {
if(lhm.containsKey(i)) {
lhm.put(i, lhm.get(i) + 1);
} else {
lhm.put(i, 1);
}
}
Set<Integer> keySet = lhm.keySet();
for(int j:keySet) {
if(lhm.get(j)>1) {
System.out.println(j + " is repeated " + lhm.get(j) + " times.");
}
}
}
public static String reverseGivenString(String input) {
String strTemp = "";
for(int i=input.length()-1; i>=0; i--) {
strTemp += "" + input.charAt(i);
}
return strTemp;
}
public static void main(String[] args) {
String str = "This is test program";
for(String s:str.split(" ")) {
System.out.print(reverseGivenString(s) + " ");
}
}
public static void main(String[] args) {
String s = "()()()";
String[] arrS = s.split("");
int count = 0;
ArrayList<String> al = new ArrayList<>(Arrays.asList(arrS));
for(int i=0; i<al.size(); i++) {
if(al.get(i).equals("(")) {
al.remove(i);
count++;
}
}
if(al.size()==count) {
System.out.println("Valid arithmetic expression");
} else {
System.out.println("Not an arithmetic expression");
}
}
public static void main(String[] args) {
String str = "the world is the here the huge here hi the";
String w1 = "the", w2 = "the";
String[] arrSplit = str.split(" ");
int dist = 0;
ArrayList<Integer> al = new ArrayList<>();
boolean distFound = false;
for(int i=0; i<arrSplit.length; i++) {
if(arrSplit[i].equals(w1)) {
for(int j=i+1; j<arrSplit.length; j++) {
if(arrSplit[j].equals(w2)) {
distFound = true;
break;
} else {
dist++;
}
}
if(distFound) {
al.add(dist);
distFound = false;
dist = 0;
}
}
}
if(al.isEmpty()) {
System.out.println("Either one of given words is not found!");
} else {
Collections.sort(al);
System.out.println(al.get(0));
}
}
public static void main(String[] args) {
int[] a = {1,2,3,4,5,6};
int[] b = {2,3,4,5,6,7,8};
if(a.length>b.length) {
int[] temp = a;
a = b;
b = temp;
}
boolean arraysEqual = false;
if(a.length == b.length) {
arraysEqual = true;
}
ArrayList<Integer> al = new ArrayList<>();
int tempSum = 0;
for(int i=0; i<a.length; i++) {
tempSum = a[i] + b[i];
if(tempSum > 9) {
al.add(tempSum/10);
al.add(tempSum%10);
} else {
al.add(tempSum);
}
}
if(!arraysEqual) {
for(int j = a.length;j<b.length; j++) {
al.add(b[j]);
}
}
System.out.println(al);
//If they want the values in ARRAY only, then uncomment below and print
/*Integer[] arr = new Integer[al.size()];
arr = al.toArray(arr);*/
}
public static void main(String[] args) {
String s = "abbcdc";
//Output should be like (a,1), (b,2), (c,2), (d,1)
LinkedHashMap<Character, Integer> lhm = new LinkedHashMap<>();
for(int i=0; i<s.length(); i++) {
if(lhm.containsKey(s.charAt(i))) {
lhm.put(s.charAt(i), lhm.get(s.charAt(i)) + 1);
} else {
lhm.put(s.charAt(i), 1);
}
}
Set<Character> keySet = lhm.keySet();
String finalVal = "";
for(Character ch:keySet) {
finalVal += "(" + ch + "," + lhm.get(ch) + "),";
}
System.out.println(finalVal.substring(0, finalVal.length()-1));
}
public static void main(String[] args) {
int[] a= {-1,-2, 1,2,3,5,-9};
int maxProd =0, temp, i1=0, i2=0, i3=0;
for(int i=0; i<a.length; i++) {
for(int j=i+1; j<a.length; j++) {
for(int k=j+1; k<a.length; k++) {
temp = a[i] * a[j] * a[k];
if(temp>maxProd) {
maxProd = temp;
i1 = a[i];
i2 = a[j];
i3 = a[k];
}
}
}
}
System.out.println("Max product is " + maxProd + " with values: " + i1 + " * " + i2 + " * " + i3);
}
I could think of 3 options below:
1) The one user Tal suggested at first --> sum of Array1 - Sum of Array2
2) Using linear search to go through each element of both the arrays and find what's missing
3) Another linear approach, but this time convert the arrays to ArrayList and use contains function to find whether the element is exist or not
public static Integer countDuplicateCharacters(String input, char ch) {
int count = 0;
for(int i=0; i<input.length(); i++) {
if(input.charAt(i) == ch) {
count++;
}
}
return count;
}
public static void printMaxOccuredChar(LinkedHashMap<Character,Integer> lhm) {
int max = 0;
String maxOccured = "";
Set<Character> keys = lhm.keySet();
for(Character ch:keys) {
if(max < lhm.get(ch)) {
max = lhm.get(ch);
maxOccured = "" + ch;
}
}
System.out.println(maxOccured);
}
public static void main(String[] args) {
String str = "HELLO O E";
str = str.replaceAll(" ", "");
LinkedHashMap<Character,Integer> lhm = new LinkedHashMap<>();
for(int i=0; i<str.length(); i++) {
if(!lhm.containsKey(str.charAt(i))) {
lhm.put(str.charAt(i), countDuplicateCharacters(str, str.charAt(i)));
}
}
System.out.println(lhm);
printMaxOccuredChar(lhm);
}
public static void main(String[] args) {
int[] a = {1,0,1,12,-2,-8};
int sum1 = 0, sum2 = 0;
for(int i=1; i<a.length; i++) {
for(int j = 0; j<=i; j++) {
sum1 += a[j];
}
for(int k=i+1; k<a.length; k++) {
sum2 += a[k];
}
if(sum1 == sum2) {
System.out.println("Sum1: " + sum1);
for(int l =0; l<=i; l++) {
System.out.println(a[l]);
}
System.out.println("Sum2: " + sum2);
for(int l =i+1; l<a.length; l++) {
System.out.println(a[l]);
}
}
sum1 =0; sum2 = 0;
}
}
After above all use cases, I would try to dig around browser compatibility.
1) Clear cookies & history of the test browser and try to hit search
2) Try what are all the supported versions of browser we use with given application
3) If the above result is failure, then check the corresponding logs and report as bug for that particular browser
4) Try with different other browsers and very the search functionality and drill down the issue whether it's specific to any browser or with all browsers in mobile
5) If above all correct and still user is not able to get search result, then check OS version and its compatibility with the application
6) Check whether user is trying access the site from unauthorised network / domain / IP / country
7) Check whether page is fully loaded or got struck, there might be some performance leakage
Likewise, the debugging steps can go on until we drill down and figure out the actual issue.
public static Boolean isPrime(int n) {
//Converting the negative numbers to positive, for better usage
if(n<0) {
n = 0-n;
}
for(int i=2; i<n/2; i++) {
if(n%i == 0) {
return false;
}
}
return true;
}
public static ArrayList<Integer> findPrimeNumbers(int[] a) {
ArrayList<Integer> alPrimeNumbers = new ArrayList<>();
for(int i=0;i<a.length;i++) {
if(isPrime(a[i])) {
alPrimeNumbers.add(a[i]);
}
}
return alPrimeNumbers;
}
public static void minimumDifference(ArrayList<Integer> al) {
Collections.sort(al, Collections.reverseOrder());
int diff;
int min = al.get(0) - al.get(1);
for(int i=0; i<al.size()-1; i++) {
diff = al.get(i) - al.get(i+1);
min = diff < min ? diff : min;
}
System.out.println(min);
}
public static void main(String[] args) {
int[] a = {2,3,5,7,8};
ArrayList<Integer> al = findPrimeNumbers(a);
minimumDifference(al);
}
public static void main(String[] args) {
int[] a = {1,2,5,9,6,8};
boolean blnFlagLess = true;
boolean blnFlagGreat = true;
for(int i=1; i<a.length; i++) {
for(int j=0; j<i; j++) {
if(a[i]<a[j]) {
blnFlagLess = false;
}
}
for(int k=i+1; k<a.length; k++) {
if(a[i]>a[k]) {
blnFlagGreat = false;
}
}
if(blnFlagLess && blnFlagGreat) {
System.out.println(a[i]);
blnFlagLess = true;
blnFlagGreat = true;
}
}
}
Here output will be 2 & 5.
- GK April 30, 2019public static void main(String[] args) {
int[] a1 = {1,1,1,2,2,2,2,3,4,4,4};
int[] a2 = {1,2};
ArrayList<Integer> alSortedMerge = new ArrayList<>();
for(int i=0; i<a1.length; i++) {
alSortedMerge.add(a1[i]);
}
int ptr = -1;
for(int j=0; j<a2.length; j++) {
for(int k=0; k<a1.length; k++) {
if(alSortedMerge.get(k)==a2[j]) {
ptr = k;
break;
}
}
if(ptr>=0) {
alSortedMerge.add(ptr, a2[j]);
ptr = -1;
} else {
alSortedMerge.add(a2[j]);
}
}
System.out.println(alSortedMerge);
}
public static String reverseString(String strInput) {
String strRev = "";
for(int i=strInput.length()-1; i>=0; i--) {
strRev += "" + strInput.charAt(i);
}
return strRev;
}
public static void main(String[] args) {
String str = "123456789";
int i1 = 3, i2 = 5;
System.out.println(reverseString(str.substring(i1, i2)));
System.out.println(reverseString(str.substring(i1)));
}
public static String compressString(String strInput) {
char[] chInput = strInput.toCharArray();
char chTemp = chInput[0];
String strCompressed = "";
int ind = 1;
for(int i=1; i<chInput.length; i++) {
if(chTemp == chInput[i]) {
ind++;
} else {
strCompressed += "" + chTemp + ind;
ind = 1;
chTemp = chInput[i];
}
}
strCompressed += "" + chTemp + ind;
return strCompressed;
}
public static String deCompressString(String strCompInput) {
String strDecompressed = "";
for(int i=0; i<strCompInput.length(); i = i+2) {
for(int j=0; j<Character.getNumericValue(strCompInput.charAt(i+1)); j++) {
strDecompressed += "" + strCompInput.charAt(i);
}
}
return strDecompressed;
}
- GK April 30, 2019What do the interviewer exactly mean by do write program without using any in-built functions?
Is it inclusive of all the library functions like substring, charat,.. and so on or that would be only to avoid using the functions like 'sort' or 'reverse' which would help us in achieving the given problem in a few lines?
Adding the missing method collectRestOfWords
- GK May 03, 2019