Google Interview Question for SDE1s


Country: United States




Comment hidden because of low score. Click to expand.
1
of 1 vote

class Data{
	Map<Character,Integer> elems;
	int idx;
	int mult;
	
	Data(Map<Character,Integer> mp, int i, int m){
		elems = mp;
		idx = i;
		mult = m;
	}
}

public Map<Character,Integer> elemCounts(String str){

	Data d = elemHelp(str, 0);
	if(d.mult != 1){
		for(Map.Entry<Character,Integer> e: d.elems.entrySet()){
			e.getValue() *= d.mult;
		}
	
	}
	return d.elems;
}

private Data elemHelp(String str, int i){
	Map<Character,Integer> mp = new HashMap<Character,Integer>();
	int mult = 1;
	while(i< str.length()){
		if(str.charAt(i) >= 'A' && str.charAt(i) <= 'Z'){
			char elem = str.charAt(i);
			int ct = 1;
			if(i + 1 < str.length() && str.charAt(i + 1) >= '0' && str.charAt(i + 1) <= '9'){
				int j = i + 1;
				while(j < str.length() && str.charAt(j) >= '0' && str.charAt(j) <= '9'){
					j++;
				}
				ct = Integer.parseInt(str.substring(i + 1, j));
				i = j;
			}else{
				i++;
			}
			if(mp.containsKey(elem)){
				ct += mp.get(elem);
			}
			mp.put(elem,ct);
		}else if(str.charAt(i) == '('){
			Data d = elemHelp(str, i + 1);
			for(Map.Entry<Character,Integer> e : d.elems.entrySet()){
				e.getValue() *= d.mult;
				if(mp.containsKey(e.getKey())){
					e.getValue() += mp.get(e.getKey());
				}
				mp.put(e.getKey(),e.getValue());
			}
			i = d.idx;
		}else{
			if(i + 1 < str.length() && str.charAt(i + 1) >= '0' && str.charAt(i + 1) <= '9'){
				int j = i + 1;
				while(j < str.length() && str.charAt(j) >= '0' && str.charAt(j) <= '9'){
					j++;
				}
				mult = Integer.parseInt(str.substring(i + 1, j));
				i = j;
			}else{
				i++;
			}
			break;
		}
		
	}
	return new Data(mp,mult,i);
}

- divm01986 April 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main (String[] args) throws java.lang.Exception
    {
          //String str = "C6H6((CH2)6(NO2)3)4";
          String str = "C6H6(CH2)6(NO2)3";
          Map<Character,Integer> map = new HashMap<>();
          map = createChemicalMap(str);
          for(Character c : map.keySet()){
              System.out.println(c+" : " + map.get(c));
          }
    }
 
    
    
    public static Map<Character,Integer> createChemicalMap(String str){
           Map<Character,Integer> map = new HashMap<>();
           if(str == null || str.length()<1){
               return map;
           }
        
           char[] array= str.toCharArray();
           Deque<Map<Character,Integer>> deque = new ArrayDeque<>();
           int num = 0;
           for(int i=0;i<array.length;i++){
               if(Character.isDigit(array[i])){
                     char pre_c = array[i-1];
                     
                     num = array[i] - '0';
                     while(i+1<array.length && Character.isDigit(array[i+1])){
                        num = num*10 + array[i+1]-'0';
                        i++;
                    }
                    if(pre_c!=')'){
                        map.put(pre_c,map.get(pre_c)*num);
                    }else{
                        for(char c : map.keySet()){
                            map.put(c,map.get(c)*num);
                        }
                        if(!deque.isEmpty()){
                            Map<Character,Integer> pre_map = deque.pollLast();
                            for(Character c : map.keySet()){
                                pre_map.put(c,pre_map.getOrDefault(c,0)+map.get(c));
                            }
                            map = pre_map;
                        }
                    }
               }else if(array[i] == '('){
                     deque.offer(map);
                     map = new HashMap<>();
               }else if(array[i]!=')'){
                   if(!map.containsKey(array[i])){
                       map.put(array[i],1);
                   }
               }
           }
           
           return map;
        
    }

- tiandiao123 September 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main (String[] args) throws java.lang.Exception
    { 
          String str = "C6H6(CH2H)6(NO2)3";
          Map<Character,Integer> map = new HashMap<>();
          map = createChemicalMap(str);
          for(Character c : map.keySet()){
              System.out.println(c+" : " + map.get(c));
          }
    }
    
    
    
    public static Map<Character,Integer> createChemicalMap(String str){
           Map<Character,Integer> map = new HashMap<>();
           if(str == null || str.length()<1){
               return map;
           }
        
           char[] array= str.toCharArray();
           Deque<Map<Character,Integer>> deque = new ArrayDeque<>();
           int num = 0;
           for(int i=0;i<array.length;i++){
               if(Character.isDigit(array[i])){
                     char pre_c = array[i-1];
                     
                     num = array[i] - '0';
                     while(i+1<array.length && Character.isDigit(array[i+1])){
                        num = num*10 + array[i+1]-'0';
                        i++;
                    }
                    if(pre_c!=')'){
                        map.put(pre_c,map.get(pre_c)+num-1);
                    }else{
                        for(char c : map.keySet()){
                            map.put(c,map.get(c)*num);
                        }
                        if(!deque.isEmpty()){
                            Map<Character,Integer> pre_map = deque.pollLast();
                            for(Character c : map.keySet()){
                                pre_map.put(c,pre_map.getOrDefault(c,0)+map.get(c));
                            }
                            map = pre_map;
                        }
                    }
               }else if(array[i] == '('){
                     deque.offer(map);
                     map = new HashMap<>();
               }else if(array[i]!=')'){
                    map.put(array[i],1+map.getOrDefault(array[i],0));
                   
               }
           }
           
           return map;
        
    }

- tiandiao123 September 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public static void main(String args[]) {
		String chemicals = "C6CCH2(N021)H20";
		HashMap<Character, Integer> result2 = getcount(chemicals);
		System.out.println("Chemical hashmap: " + result2);
}
public static HashMap<Character, Integer> getcount(String chemicals){
		HashMap<Character, Integer> hmap = new HashMap<Character, Integer>();
		int value = 0;
		
		for(int i = 0; i < chemicals.length(); i++) {
			char key = chemicals.charAt(i);
			
			if(hmap.containsKey(key)){
				value = hmap.get(key);
				hmap.put(key, ++value);
			}else {
				hmap.put(key, 1);
			}
		}
		System.out.println("hmap: " + hmap.toString());
		return hmap;
		
}

- KimberlyT April 12, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More