Amazon Interview Question for Software Engineer / Developers






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

#include<iostream>
#include <strstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
void convert(char *input)
{
char *start = input;
char *s1 = input;
char *s2 = input + 1;

int len = strlen(input);
int count = 0;
char *end = start;
while(--len && *s1 && *s2 )
{
while(*s1 && *s2 && len && *s1 == *s2)
{
++count;
s2++;
--len;
}
if(count)
{
ostrstream str;
str<<(count+1);
*start = *s1;
*++start = *(str.str());
start = start + strlen(str.str()) - 1;
count = 0;
}
else
*start = *s1;
start++;
s1 = s2;
s2++;
}
if(*s1 != *(s1-1))
*start++ = *s1;
*start = *s2;
cout<<"Magic: "<<end<<endl;
return;
}
int main()
{
char input[100];
memset(input, 0, 100);
cout<<"Enter the string: ";
scanf("%s", input);
cout<<"You entered: "<<input<<"!!\n";
convert(input);
return 0;
}

Guess we can still optimise the above program. Can someone give a try ?

- Ganesh December 22, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isn't this better.
You might have to input files to convert number to string(itoa) and I don't know if I have used right strcat.
But just trying to shorten the logic here.
void convert(char *string)
{
char *encode;
int count = 0;
char tmp;
while( string)
{
tmp = *string++;
count++;
if (tmp != *string)
{
*encode++ = tmp;
string numb = itoa(count);
strcat(encode,numb);
count = 0;
}
}
}
int main()
{
char input[100];
cout<<"Enter the string: ";
scanf("%s", input);
cout<<"You entered: "<<input<<"!!\n";
convert(input);
return 0;
}

- Anonymous December 25, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

adding on your idea: (now this works)

- itoa can't be used. It is non-ISO function. Also not defined in stdlib.h
- For now assuming that no letter will repeated more than 9 times. Hence our count will not exceed 9.

codE:

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

using namespace std;

char *convert(char *string)
{
static char output[53]; // assuming 26 different in input; last one for '\0'

int count = 0, track=0;
char digit;

while(*string)
{
output[track] = *string++;
count++;
if (output[track] != *string) // //compare with next letter in string
{
track++;
if (count > 1) // block not reqd //when no repetitions (abb should be abb2)
{
digit = count + 48; // //adding ASCII value for the digit
output[track++] = digit;
}
count = 0;
}
}
output[track] = '\0'; // mark end of output //array, not reqd since static initializes to repetitive //'\0'
return(output);
}


int main()
{
char input[100];
char *now;
cout<<"Enter the string: ";
scanf("%s", input);
cout<<"You entered: "<<input<<"!!\n";
now = convert(input);
cout<<"Final Output: "<<now<<endl;
return 0;
}

Note:
- To allow more than 9 repetitions, we can implement a separate itoa function

- raki December 28, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Some bugs cleared


#include <iostream.h>
#include <string.h>
#include <conio.h>


char *convert(char *string)
{
static char output[53]; // assuming 26 different in input; last one for '\0'

int count = 0, track=0;
char digit;

while(*string)
{
output[track] = *string++;
count++;
if (output[track] != *string) // //compare with next letter in string
{
track++;
if (count > 1) // block not reqd //when no repetitions (abb should be abb2)
{
digit = count + 48; // //adding ASCII value for the digit
output[track++] = digit;
}
count = 0;
}
}
output[track] = '\0'; // mark end of output //array, not reqd since static initializes to repetitive //'\0'
return(output);
}


int main()
{
char input[100];
char *now;
cout<<"Enter the string: ";
cin>>input;
cout<<"You entered: "<<input<<"!!\n";
now = convert(input);
cout<<"Final Output: "<<now<<endl;
getch();
}

- Anonymous September 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Actually itoa should work fine with this.
#include <iostream>
#include <string>
using namespace std;
char* convert(char *string)
{
char encode[50];
int count = 0,track =0;
char tmp;
char b[50];
char* numb;
while(*string)
{
tmp = *string++;
count++;
if (tmp != *string)
{
encode[track] = tmp;
track++;
numb = itoa(count,b,10);
encode[track++] = *numb;
count = 0;
}
}
encode[track] = '\0';
return encode;
}
int main()
{
char input[100];
char* converted;
memset(input, 0, 100);
cout<<"Enter the string: ";
cin >> input;
cout<<"You entered: "<<input<<"!!\n";
converted = convert(input);
return 0;
}

- Anonymous January 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This sounds similar to RunLength Algorithm
pseudocode:
str = input string;
char cur;
i=0, count;
while(i!=strlen(str)){
cur = str[i];count = 1;
while (cur == a[i+count]){
count++;
}
print str[i];
if(count > 1){
print count;
}
i = i + count;
}

- Anonymous January 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

my solution

void encode(char *s)
{
int i,j=0,len=strlen(s);
int count;

for(i=0;i<len;i++){
s[j++]=s[i];

if(s[i]==s[i+1]){
count=2;
i++;
while(s[i]==s[i+1]){
i++;count++;
}
s[j++]=count+'0';
}

}
s[j]='\0';
}

- Hendry January 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Opps, the above solution crashed when the number of duplicate exceed 10.

void encode(char *s)
{
int i,j=0,len=strlen(s);
int count;
char buffer[10];
int index;
char n;

for(i=0;i<len;i++){
s[j++]=s[i];

if(s[i]==s[i+1]){
count=2;
i++;
index=0;
while(s[i]==s[i+1]){
i++;count++;
}
while(count){
n=count%10+'0';
count/=10;
buffer[index++]=n;
}

index--;
while(index>=0)
s[j++]=buffer[index--];
}

}
s[j]='\0';
}

- Hendry January 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void code(std::string str) //Complexity o(n*n)
{
std::map<std::string,int> mp;
std::map<std::string,int>::iterator ii;
int len=str.length();
char *s=&str[0];
int count=0;
while(count<len)
{
char ch=*(s+count);
std::string sd(&ch,1);
mp[sd]=0;
count++;
}
count=0;
for(ii=mp.begin(); ii!=mp.end(); ++ii)
{
std::string ss=(*ii).first;
while(count<len)
{
char ch=*(s+count);
std::string sd(&ch,1);
if(!ss.compare(sd))
{
(*ii).second++;
}
count++;
}
count=0;
}
for(ii=mp.begin(); ii!=mp.end(); ++ii)
{
std::cout << (*ii).first << ": " << (*ii).second << std::endl;
}
}

- Abhas February 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void main()
{
int hash[256];
char *str="aaabbbbbbccccddddeeeeeee";
for(int i=0;i<256;i++) hash[i]=0;
int len=strlen(str);
for(i=0;i<len;i++)
{
hash[str[i]]++;
}
char buf[100];
char *p=buf;
for(i=0;i<256;i++)
{
if(hash[i])
{
len=sprintf("%c%d",i,hash[i]);
}
p = p + len;
}
p = NULL;
printf("%s\n",buf);
}

- Anonymous March 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
sdv e qe qe efqetqe
}

- Anonymous March 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void main()
{
  int hash[256];
  char *str="aaabbbbbbccccddddeeeeeee";
  for(int i=0;i<256;i++) hash[i]=0;
  int len=strlen(str);
  for(i=0;i<len;i++)
  {
    hash[str[i]]++;
  }
  char buf[100];
  char *p=buf;
  for(i=0;i<256;i++)
  {
    if(hash[i])
    {
      len=sprintf(buf,"%c%d",i,hash[i]);
    }
    p = p + len;
  }
  p = NULL;
  printf("%s\n",buf);
}

sprintf writes to the buffer formatted strings... so, no tensions for itoa and all.. the code also runs in O(n)... best possible soln i can think of...

- Saptarshi March 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

some minor bugs corrected

void main()
{
  int hash[256];
  char *str="aaabbbbbbccccddddeeeeeee";
  for(int i=0;i<256;i++) hash[i]=0;
  int len=strlen(str);
  for(i=0;i<len;i++)
  {
    hash[str[i]]++;
  }
  char buf[100];
  char *p=buf;
  for(i=0;i<256;i++)
  {
    if(hash[i])
    {
      len=sprintf(p,"%c%d",i,hash[i]);
      p = p + len;
    }
  }
  p = NULL;
  printf("%s\n",buf);
}

sprintf writes to the buffer formatted strings... so, no tensions for itoa and all.. the code also runs in O(n)... best possible soln i can think of...

- Saptarshi March 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice!
i should be declared as unsigned int

- anon November 10, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void codeString(char s[])
{
	int size = strlen(s);
	int i, len=1, index=0;
	char temp = s[0];

	for (i=1; i< size; i++)
	{
		if (s[i] == temp )
		{
			len++;
			continue;
		}else
		{
			if (len > 1)
			{
				s[index++]= temp;
				s[index++]= '0'+ len;
				temp = s[i];
				len=1;
			}else
			{
				s[index++]= temp;
				temp = s[i];
			}
		}

	}

	s[index++]= temp;
	if (len >1)
		s[index++]= '0'+ len;
	s[index]='\0';
}

- John March 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void codeString(char s[])
{
	int size = strlen(s);
	int i, len=1, index=0;
	char temp = s[0];

	for (i=1; i< size; i++)
	{
		if (s[i] == temp )
		{
			len++;
			continue;
		}else
		{
			if (len > 1)
			{
				s[index++]= temp;
				s[index++]= '0'+ len;
				temp = s[i];
				len=1;
			}else
			{
				s[index++]= temp;
				temp = s[i];
			}
		}

	}

	s[index++]= temp;
	if (len >1)
		s[index++]= '0'+ len;
	s[index]='\0';
}

- John March 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String args[]) {
		String s = "abbbccddddeee";
		char[] ca = s.toCharArray();
		String s_ = "";
		for(int i=0; i<ca.length; i++) {
			s_ = s_ + ca[i];
			int j=1;
			for(;j+i<ca.length; j++) {
				if(ca[i+j] != ca[i]) {
					break;
				}
			}
			if(j > 1) {
				s_ = s_ + j;
				i += j - 1;
			}
		}
		System.out.println(s_);
	}

- Anonymous April 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

std::string compress(std::string str){
	std::string result="";
	int i=0;
	while(i<str.length()){
		int count=1;
		result.push_back(str[i]);
		while((str[i]==str[i+1])&&(i<str.length())){
			++count;
			i++;
		}
		if(count>1){
			char buffer[4];
			memset(buffer,'\0',1);
			itoa(count,buffer,10);
			std::string temp(buffer);
			result.append(buffer);
			//result.push_back(temp.c_str());
		}
		i++;
	}
	return result;
}

- Golu September 13, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<string>
using namespace std;
void check(char * q)
{
int count=1;
char p;
        while(*q != '\0' )  {
        p=*q++;
                if(p != *q)  {
                cout<<*(q-1)<<count;
                count = 1;
                }
                else  count++;
        }
}

int main()
{
char *s ="abbbbbbbtttdddgggwwwnnnyyy";
cout<<s<<endl;
check(s);
return 0;

}

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

It is simple

#include<stdio.h>
int main()
{
char string[60],i,j,count;
printf("Enter a string\n");
scanf("%s",&string);
for(i=0;i<=strlen(string);i++)
{
count=0;
for(j=i;j<=strlen(string);j++)
{
if(string[i]==string[j]) ++count;
else 
{
if(count==1) printf("%c",string[i]);
else printf("%c%d",string[i],count);
i=i+count-1;
break;
}
}
}
getch();
return 0;
}

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

void encode ( char* str ){
    char *str = "aaaaabccc";
    int count = 1;  
    for( int i=0,j=1; i < strlen(str); i++, j++){         
         if( str[i] == str[j] )
                   count++;
         else{
                   cout << str[i] << count;
                   count = 1;
              }
    }
}

- insaneish October 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

why are some of you guys just printing the string with the number of time it is repeated? You have to modify the string, not just print to the console. It's just plain stupid and dumb...

I know I am not helping anybody without writing any code...but it is better not to write anything rather than some stupid logic

- veeru December 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

package com.amazon.interview.general;

public class CompactString {

	public static void main(String[] args) {
		String str = "aabbbcddeefggg";
		System.out.println(compact(str));
		System.out.println(compact(null));
		System.out.println(compact("a"));
		System.out.println(compact("aa"));
		System.out.println(compact("ab"));
		System.out.println(compact("aabb"));
		System.out.println(compact("abcedf"));
		System.out.println(compact("aabbccddeeefffggg"));
		System.out.println(compact("abccc"));
	}

	private static String compact(String str) {
		System.out.println(str);
		if (str == null)
			return null;
		
		if (str.length() < 1) 
			return str;
		
		StringBuilder sb = new StringBuilder();
		char ch = str.charAt(0);
		int count  = 0;
		for (char t : str.toCharArray()) {
			if ( t == ch) {
				count ++;
			} else {
				sb.append(ch);
				sb.append(count);
				ch = t;
				count = 1;
			}
		}
		sb.append(ch);
		sb.append(count);
		return sb.toString();
	}

}

- Madhu May 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Time O(n) and Space complexity using a buffer
public static void numberString()
{
String str = "abbbccddddeee";
StringBuilder build = new StringBuilder();
build.append(str.charAt(0));
int charCount = 1;
for (int i = 1; i < str.length(); i++)
{
if ( str.charAt(i) == str.charAt(i-1))
charCount++;
else
{
if (charCount > 1)
{
build.append(charCount);
charCount = 1;
}
build.append(str.charAt(i));
}
}
if ( charCount > 1)
build.append(charCount);

System.out.println("Modified String: " + build.toString());
}

- Kalyan October 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void compress(char a[]){
    
    int len = strlen(a), i, j = 0, count = 1;
    for(i = 1; i <= len; i++){
        if(a[i] != a[j]){
            if(count > 1 && count < 10)
                a[++j] = 48 + count;
            a[++j] = a[i];
            count = 1;
        }
        else
            count++;
    }
    a[++j] = '\0';
}

- Anonymous July 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This works till count <=9

- Anonymous July 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Really simple solution her:

github.com/codeprepper/data-structures/blob/master/compressString/answer.c

Test cases are at:

github.com/codeprepper/data-structures/blob/master/compressString/test.c

- CodePrepper July 25, 2013 | 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