Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

{{
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define HASHINFO(x) hash[x] = 1;
#define GETHASH(x) hash[x]
int hash[256];
void replace(char * str, char * delim)
{
memset(hash,0,sizeof(hash));
while(*delim)
{
HASHINFO(*delim);
delim++;
}
while(*str)
{
if(GETHASH(*str))
{
*str = '|';
}
str++;
}

}
void driver()
{
char string[50] = {0};
char delim[50] = {0};

printf("Enter the string\n");
gets(string);
printf("Enter the delims\n");
gets(delim);
replace(string,delim);
printf("\n Result = \n%s",string);
}

void main()
{
driver();
}
}}

- Anonymous November 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good technique!!

- Anonymous November 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

best technique!

- Anonymous November 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will add bunch of empty entries in the list if there are 2 or more continuous delims.
eg. Mr. X should output Mr|X and not Mr||X

but yeah, its not mentioned in the question if output expected should be either of them

- Anonymous August 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

Here is another solution,

puddleofriddles.blogspot.com/2011/11/write-function-to-replace-certain.html

- Anonymous January 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

mport java.util.regex.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
public class MultiDelimiter{
public static void main(String args[]){
String val="How are you,Mr.X?";
List<String> delList=new ArrayList<String>();
delList.add(".");
delList.add(",");
delList.add("\\s");
List<String> result=method(val,delList);
System.out.println("result"+result);
}

public static List<String> method(String s,List<String> delimiterList){
String delimiters="[";
for(String del:delimiterList){
delimiters=delimiters+del;
}
delimiters=delimiters+"]+";
String[]str=s.split(delimiters);
List<String> list=Arrays.asList(str);
return list;
}
}

- Ashwani Kumar Tripathi November 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think, this is what is required and not the other solution!!

- gaurav.in November 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Should be straight forward as below

public void output(String abc, List delims){
int len = abc.length();
StringBuilder stb = new StringBuilder(abc);
StringBuilder newWord = new StringBuilder();
int i = 0; 
while(i<len){
while(i<len && delims.contains(stb.charAt(i)))
i++;

while(i<len && !delims.contains(stb.charAt(i)))
{newWord.append(stb.charAt(i));
i++;
}

if(i<len && delims.contains(stb.charAt(i)))
newWord.append('|');
}
return newWord.toString();
}

- ishantagarwal1986 November 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

its more or less pattern search question. one can use rabin karp or Knuth Morris pratt or smthing else for better time complexity.

- Anonymous November 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// algorithm.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <cstring>


void DevideString
(
	char szText[], 
	size_t nTextLength, 
	const char Delimiters[], 
	size_t nNoDlmt
)
{
	int nDlmtCombined = 0;

	for(size_t n = 0; n < nNoDlmt; ++n)
	{
		nDlmtCombined |= Delimiters[n];
	}

	for(size_t idx = 0; idx < nTextLength; ++idx)
	{
		if( szText[idx] == (szText[idx] & nDlmtCombined) )
		{
			szText[idx] = '|';
		}
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	const char Delimiters[] = {' ', ','};
	char szMessage[] = "This is Wade, from China!";

	

	DevideString(szMessage, strlen(szMessage), Delimiters, sizeof(Delimiters)/sizeof(char));

	printf("%s\n", szMessage);

	return 0;
}

- Anonymous November 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it's generally not gonna work:
suppose you have two delimiters 'a' and 'b' and a character
in the string with ASCII code 'a|b', then your would find a match
where it shouldn't

- Anonymous November 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <string.h>
#include <stdio.h>

void StringModifier(char str[], char delims[])
{
int m = strlen(str);
int n = strlen(delims);
int i=0,j=0;
printf("Initial String %s\n", str);
while(i<m)
{
j=0;
while(j<n)
{
if(str[i] == delims[j])
{
str[i] = '|';
break;
}
else
j++;
}
i++;
}
printf("Modified String %s\n", str);
return;
}

- kmrinalinipriyanka November 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is too much time consuming with a o(mn) time complexity. you are checking each character of the string with each character of delims, so each character of delims is getting testes multiple times, waste of time.
also if you are going to use string library, then read about strtok function, why not use it?

- Anonymous November 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class stringdel {

public static void main(String[] args)
{
stringdel sd = new stringdel();
char[] a = {' ','?','.',','};
sd.delimit("Hello how r you, Mr.X??",a);
}
public void delimit(String s, char[] a)
{
int str_len= s.length();
char[] str = new char[str_len];
str = s.toCharArray();
int i,j;
for (i=0;i<str_len;i++)
{
for (j=0;j<a.length;j++)
{
if (str[i] == a[j])
{
str[i]='|';
break;
}
}

System.out.print(str[i]);
}

}
}

- guru November 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the question is to tokenize the string based on delimiters and return a list of string(each token as a item in the list) and not to replace the delimiters with '|'....

- Gaurav November 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

putting the delimiter characters in hash table is a good approach , with this ques can be solved in O(n) .

- kamal November 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>

void fn(char sti[], char deli[]);

void fn(char sti[], char deli[])
{
int i, j, k = 0;
for(i = 0; deli[i] != '\n'; i++)
{
for(j = 0; j < 50; j++)
{
if(deli[i] == sti[j])
sti[j] = '|';
}
}
printf("The newly formed string:\n");
while (sti[k] != '\n')
{
putchar(sti[k]);
k++;
}
}
void main()
{
int i = 0;
char sti[50], sto[50], deli[20];
printf("\nEnter the string\n:");
//for(i = 0; i < 50; i++)

while ((sti[i] = getchar()) != '\n')
{
i++;
}
printf("\nEnter the delimeters:\n");
i = 0;
while ((deli[i] = getchar()) != '\n')
{
i++;
}
fn(sti, deli);
system("pause");
}

- Prathamesh November 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>

void fn(char sti[], char deli[]);

void fn(char sti[], char deli[])
{
int i, j, k = 0;
for(i = 0; deli[i] != '\n'; i++)
{
for(j = 0; j < 50; j++)
{
if(deli[i] == sti[j])
sti[j] = '|';
}
}
printf("The newly formed string:\n");
while (sti[k] != '\n')
{
putchar(sti[k]);
k++;
}
}
void main()
{
int i = 0;
char sti[50], sto[50], deli[20];
printf("\nEnter the string\n:");
//for(i = 0; i < 50; i++)

while ((sti[i] = getchar()) != '\n')
{
i++;
}
printf("\nEnter the delimeters:\n");
i = 0;
while ((deli[i] = getchar()) != '\n')
{
i++;
}
fn(sti, deli);
system("pause");
}

- Prathamesh November 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the Complete java code:

import java.util.*;
import java.lang.*;

class CheckDelimiters {
public static String s = "How are you,Mr.X?";
public static char[] delimiters = {' ',',','.'};
Set h = new HashSet();
void check(String s,char[] delimiters){
System.out.println("Given String:" + s);
for(int i=0;i<delimiters.length;i++) {
h.add(delimiters[i]);
}
for(int i=0;i<s.length();i++) {
if(h.contains(s.charAt(i))) {
s = s.replace(s.charAt(i),'|');
continue;
}
}
System.out.println("Modified String:" + s);
}
public static void main(String[] args) {
CheckDelimiters c = new CheckDelimiters();
c.check(s,delimiters);
}
}

- Venkat November 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess by "|" in the above question, it was meant that each word seperated by "|" would appear at one location in whatever data structure the output is stored. So, if it is a ArrayList, How would appear at position 0... etc...

- Anonymous November 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

typedef list< std::pair<string, size_t > > output;
const op tokenize(string input)
{
   output myOp;

   int len = input.length();
   int i;
   string word;
   const char * str = input.c_str();

   for(i=0; i<len; i++)
   {
      switch(str[i])
      {
        case ',':
        case '.':
        case ' ':       if(!word.empty())
                        { myOp.push_back(make_pair(word,word.length()));
                          myOp.push_back(make_pair("|",1));
                          word.clear();
                        }
                        break;
        default:
                        word.append(1,str[i]);

      }
   }

   myOp.push_back(make_pair(word,word.length()));
   return myOp;

}

- Anonymous November 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public String output(String inputString, List<Character> delimiters) {
		int len = inputString.length();
		StringBuilder newWord = new StringBuilder();
		int i = 0;
		while (i < len) {
			if (delimiters.contains(inputString.charAt(i)))
				newWord.append("|");
			 else 
				newWord.append(inputString.charAt(i));
			i++;
		}
		return newWord.toString();
	}

This solution however is O(mn) where m is the length of the delimitters and n is the length of the String ..

- Anonymous November 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String ReplaceSpeChar(String abc,List del){
StringBuilder temp = new StringBuilder(abc);
int len = temp.length();
StringBuilder temp1 = new StringBuilder();
char last_char = '\0';
for(int i=0;i<len;i++){
char c = temp.charAt(i);
if(del.contains(c)){
if(i!=0 && last_char !='|'){
temp1.append('|');
last_char = '|';
}
}
else
{
temp1.append(c);
last_char = c;
}
}
return temp1.toString();
}

- Anonymous November 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.askmesoft.gint.men;

import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

public class MyStringTokenizer {
	
	public static List<String> split(String original, List<String> separators) {
		StringBuilder separatorBuilder = new StringBuilder();
		for(String sep: separators) {
			separatorBuilder.append(sep);
		}
		List <String> result = new LinkedList<String>();
		StringTokenizer tokenizer = new StringTokenizer(original, separatorBuilder.toString());
		while(tokenizer.hasMoreTokens()) {
			result.add(tokenizer.nextToken());
		}
		return result;
	}
	
	public static void main(String[] args) {
		List<String> separators = new LinkedList <String>();
		separators.add(".");
		separators.add(",");
		separators.add(" ");
		System.out.println(split("hi,how are.you", separators));
	}
}

- Maximus November 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Dear All please check my code using hashing..
Can any one tell me the complexcity of this program..?
void delim(char *str,char *del)
{
int hash[256]={0,};
for(int i=0;del[i]!='\0';i++)
hash[del[i]]++;
for(int i=0;i<strlen(str);i++)
{
if(hash[str[i]]>=1)
str[i]='|';
}

}

- Nikhil` December 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RemoveDelimiters {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub\
LinkedList<String> list1 = new LinkedList<String>();
list1.add(".");
list1.add(" ");
list1.add("?");
list1.add(",");
createdelimeter("How are you, Mr.X?",list1);


}

public static String createdelimeter(String string, LinkedList<String>list1){

for(int i=0; i<string.length();i++){
if(list1.contains(string.substring(i,i+1)))
string = string.replace(string.charAt(i), '|');


}


System.out.println(string);
return null;
}

}

- RohitDumbre86 March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void getDelimitedString(String str){

ArrayList<String> list = new ArrayList<String>();

StringBuffer strBuf = new StringBuffer();
for(int i = 0 ; i < str.length() ; i++){

if(i > 0 && (str.charAt(i) == ',' || str.charAt(i) == '.' || str.charAt(i) ==' ') && !strBuf.equals("")){
list.add(strBuf.toString());
strBuf.delete(0, strBuf.length());
}else{
strBuf.append(str.charAt(i));
}

}

list.add(strBuf.toString());
for(String temp : list){
System.out.println(temp);
}

}

- A January 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

input = "How are you, Mr. X?"
   h = ['?', ' ' ,'.' ,',']
   for i in h:
      if i in input:
         j = input.replace(i,'|')
         input = j
   print j

- Ridhibhan.19 August 13, 2016 | 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