Amazon Interview Question: two algorithm questions, one ... | Glassdoor

Interview Question

Intern Interview

two algorithm questions, one of them is given a string

  , substitute special chars. give all possible results. like substitute 'a' and 'o' from amazon, then the possible answer should be: AmAzon, amazOn, AmAzOn
Answer

Interview Answer

1 Answer

0

import java.util.*;
public class HelloWorld{

     public static void main(String []args){
         String str="amazon";
         Map map=new HashMap();
         map.put('a', "A");
         map.put('o', "O");
        System.out.println(f(str, map));
     }

     public static Set f(String str, Map map)
     {
         Set set=new HashSet();
         f(str, set, map, 0);
         return set;
     }

     private static void f(String str, Set set, Map map, int level)
     {
         if(level>=str.length()){
             return;
         }

            String temp= updateStringAtIL(str, set, map, level);
            f(temp, set, map, level+1);
            f(str, set, map, level+1);

     }

     private static String updateStringAtIL(String str, Set set, Map map, int level){

         if(map.get(str.charAt(level)) !=null){
            StringBuilder temp=new StringBuilder(str);
            temp.replace(level, level+1, map.get(str.charAt(level)));

            str=temp.toString();
            set.add(str);
         }

         return str;
     }

Infinity on Feb 4, 2015

Add Answers or Comments

To comment on this, Sign In or Sign Up.