Tuesday, 22 November 2016

Amazon interview questions :- How to find a horoscope(relation) between two name by using "FLAMES" in Java.

This question was asked in amazon interview.

First interviewer was asked whether do you know about flames.  I said no, then he started explaining about how the "FLAMES" was working two find a horoscope between two name by giving below example.

Lets take two name "Dany" and "Diana". And strike the common character from the two name.

For Example:

Step 1: To find total length of remaining mismatched character of two name.




The above character check is case insensitive('a' is same as 'A');


Step 2: To strike character from 'FLAMES' based on the length value of Step 1.




Step 3: To find a horoscope relation between two name based on the last character from Step 2.

This can be achieved through default set values of each character of 'FLAMES'.


  • F   - Friend
  • L   - Love
  • A   - Affection
  • M  - Marriage
  • E   - Enemy
  • S    - Sister


In this case the last character is 'F'. So the horoscope relation between two name is 'Friend'
            
For above steps, interviewer wants me to write logic in Java.

Code:

public class Flames {
private static void CalCulateFlames(String first, String second) {
String flames = "flames";
for (int i = 0; i < first.length(); i++) {   // logic to strike the common letter.
String c1 = first.substring(i, i + 1);
for (int j = 0; j < second.length(); j++) {
String c2 = second.substring(j, j + 1);
if (c1.equalsIgnoreCase(c2)) {
first = first.replaceFirst(c1, "");
second = second.replaceFirst(c2, "");
i = 0;
j = second.length();
}
}
}
int total = first.length() + second.length(); // total length of remaining mismatched character.
int c = 1;
while (flames.length() != 1) {   // logic to strike the "flames" letter based on total length.
int l = 0;
while (l < flames.length()) {
if (c == total) {
flames = flames
.replaceFirst(flames.substring(l, l + 1), "");
c = 1;
} else {
c++;
l++;
}
}
}
if (flames.equalsIgnoreCase("f")) {     // logic to find horoscope relation between two name
System.out.println(" Friend ");
} else if (flames.equalsIgnoreCase("l")) {
System.out.println(" Love ");
} else if (flames.equalsIgnoreCase("a")) {
System.out.println(" Affection ");
} else if (flames.equalsIgnoreCase("m")) {
System.out.println(" Marriage ");
} else if (flames.equalsIgnoreCase("e")) {
System.out.println(" Enemy ");
} else {
System.out.println(" Sister ");
}
}

public static void main(String[] args) {
CalCulateFlames("Dany", "Diana");
}
}

Output:-

friend





If you like the above solution . Please share this blog

No comments:

Post a Comment

s