Amazon Interview Question for Software Development Managers


Country: United States
Interview Type: Phone Interview




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

The trick is to first replace all the input strings with something unique and non-colliding. So my opinion is to hash each string into a unique value, such as: input[0].hashCode() and surround that with single quotes. This should not cause collision. Then, replace all the occurrences of inputs in the text with their hashed value. And finally replace the hashed values with the replacement values.

Working example:

public class Main {
	
		public static void main(String[] args) {
			
			String[] inputs = {"teh", "quick", "rown", "Fox", "jmuped"};
			String[] replacements = {"the", "quick b", "rown ", "fox", "jumped"};
			
			String input = "teh quickrownFox jmuped over the lazy dog";
			
			letsGo(input, inputs, replacements);
		}
	
		public static void letsGo(String input, String[] inputs, String[] replacements){
			System.out.println("start: " + input);
			String[] hashes = new String[5];
			for(int i=0; i<inputs.length; i++){
				hashes[i] = "'"+inputs[i].hashCode()+"'";
			}
			for(int i=0; i<inputs.length; i++){
				String str = inputs[i];
				String replacement = hashes[i];
				input = input.replaceAll(str, replacement);
			}
			System.out.println("hashed: " + input);
			for(int i=0; i<inputs.length; i++){
				String str = hashes[i];
				String replacement = replacements[i];
				input = input.replaceAll(str, replacement);
			}
			System.out.println("final: " + input);
		}
	}

- firebluetom October 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Thanks for the sample, i am unable to understand the use of hashcode in the given coding. Below mentioned code will also give the same as we are replacing word by word.

public static void letsGo2(String input, String[] inputs, String[] replacements){
System.out.println("start: " + input);

for(int i=0; i<inputs.length; i++){
String str = inputs[i];
String replacement = replacements[i];
System.out.println("before replace : " + str +replacement);
input = input.replaceAll(str, replacement);
}
System.out.println("final: " + input);
}

- Balaji Thiruvengadam August 03, 2015 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public static void letsGo2(String input, String[] inputs, String[] replacements){
			System.out.println("start: " + input);
			
			for(int i=0; i<inputs.length; i++){
				String str = inputs[i];
				String replacement = replacements[i];
				System.out.println("before replace : " + str +replacement);
				input = input.replaceAll(str, replacement);
			}
			System.out.println("final: " + input);
		}

- Balaji Thiruvengadam August 03, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#!/bin/sh
sed '1,$ {
        s/'"$1"'/SED_FOO_1/g
        s/'"$2"'/SED_FOO_2/g
        s/'"$3"'/SED_FOO_3/g
        s/'"$4"'/SED_FOO_4/g
        s/'"$5"'/SED_FOO_5/g
        s/SED_FOO_1/'"$6"'/g
        s/SED_FOO_2/'"$7"'/g
        s/SED_FOO_3/'"$8"'/g
        s/SED_FOO_4/'"$9"'/g
        s/SED_FOO_5/'"${10}"'/g
}'

./amazon.sh black night white day hose white day black night sprinkler < test.txt

- srterpe October 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Simply scan for each input and make an in-place replacement in a string buffer. Should have a complexity of ~O(5n) or simply O(m*n) where m is the number of inputs to replace and n is the length of the body text. Average case will be faster... perhaps Θ(3n).

public static String replaceByLookup(String[] input, String[] replace, String text) {
        StringBuilder buf = new StringBuilder(text);
        int pos = 0;
        while (pos < buf.length()) {
            boolean madeReplacement = false;
            for (int i = 0; i < input.length; i++) {
                if (pos + input[i].length() > buf.length())
                    continue;
                if ( input[i].equals(buf.substring(pos, pos + input[i].length())) ) {
                    buf.replace(pos, pos + input[i].length(), replace[i]);
                    pos += replace[i].length();
                    madeReplacement = true;
                    break;
                }
            }
            if (! madeReplacement)
                pos++;
        }
        return buf.toString();
    }

- AlexSGV October 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Looks like you are missing multiple replacements. But solution looks good.

- manohar123123reddy September 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StringMatch {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String[] input = new String[5];
		String[] replace = new String[5];
		
		input[0] = "hello";
		input[1] = "is";
		input[2] = "madara";
		input[3] = "luffy";
		input[4] = "tobi";
		
		replace[0] = "hi";
		replace[1] = "was";
		replace[2] = "itachi";
		replace[3] = "dragon";
		replace[4] = "obito";
		boolean flag = false;
		String text = new String();
		text = "hello is madara there? this is tobi here";
		Scanner sc = new Scanner(text);
		String inp;
		StringBuilder out = new StringBuilder();
		while(sc.hasNext()){			
			inp = sc.next();
			for(int i=0; i<input.length;i++){
				flag = false;				
				if(inp.compareTo(input[i])==0){
					inp = replace[i];
					out.append(inp);
					out.append(" ");					
					break;
				}else{
					flag = true;
					continue;
				}					
			}
			if(flag){
				out.append(inp);
				out.append(" ");				
			}			
		}
		System.out.print(out);
	}
}

- Aswinvandev October 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use Trie data-structure to get O(n) of complexity.

class TrieNode {
public:
  TrieNode *next[256];
  char *repStr;
  TrieNode() {
    memset(next, 0, sizeof(next));
    rep_str = NULL;
  }
  ~TrieNode() {
    for (i = 0; i < 256; ++i) {
      if (next[i]) {
        delete next;
      }
    }
  }
};

TrieNode *buildTree(char **rep, char** repWith) {
  TrieNode *root = new TrieNode;
  for (int i = 0; rep[i] != NULL; i++) {
    char *c = rep[i];
    TrieNode *curr = root;
    while (*c != '\0') {
      if (curr->next[(unsigned)*c] == NULL) {
        curr->next[(unsigned)*c] = new TrieNode;
      }
      curr = curr->next[unsigned)*c];
    }
    curr->repStr = repWith[i];
  }
}

char * ReplaceStr(char *orig, char **rep, char **repWith) {
  char *buffer = new char[strlen(orig) + 1];
  int size = strlen(orig) + 1;
  int write = 0;

  TrieNode *root = buildTree(rep, repWith);
  int depth = 0;
  TrieNode *curr = root;

  while (*orig != '\0') {
    buffer[write++] = *orig;
    curr = curr->next[(unsigned)*rep];
    depth++;
    if (curr == NULL) {
      curr = root;
      depth = 0;
    } else if (curr->repStr != NULL) {
      write -= depth;
      char *c = curr->repStr;
      while (*c != '\0') {
        buffer[write++] = *c
      }
    }
  }
}

- Anonymous October 18, 2014 | 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