Amazon Interview Question for Software Engineer in Tests






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

you can use a hash table with key equal to each character and value getting incremented every time you have a key collision. That way for small strings your program would use a neat and small hashtable.

even for a million characters (when the string is a huge paragraph) the maximum the hash table can get to is 65,535 for unicode characters and a simple 255 for ascii characters.

So, even for large and small strings, the approach with hash table is easier

Test cases
1. First and foremost, when you are dealing with characters, don't forget the case sensitive issues
2. Then check for empty string
3. What to do when string is abc---result should be a1b1c1
4. what do if the string is abababcdcdcd---should it be ab3cd3 or a3b3c3d3,,,ask for it the repeated characters are side by side
5. how to deal with white space characters
6. check if the code is efficient for 1 million characters
7. what happens if the code is run for 1 month continuously
8. what about specail characters and roman numericals and etc,...
9. check for entry and exit locations of the routine
10. check for interface testing..are any unusual things happening when interfaced with a larger routine

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

Use three pointers.

void convertstr(char * str)
{
char* p1, p2, p3;
p1=p3=str;
p2=str+1;
int count=1;
int len=strlen(str);
if(len<2) return;
while(*p2!='\0')
{
if(*p=*p2)
count++;
else
{
*p3=*p1;
*(p3+1)=count;
p3=p3+2;
p1=p2;
p2=p2+1;
count=1;
}
}
*p3='\0';
}

- ohioboy January 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

private string Abbreviate(string text)
{
    string rv = "";
    int x = 0;
    for (int i = 1; i <= text.Length; i++)
    {
        if (i == text.Length || text[x] != text[i])
        {
            rv += text[x] + "" + (i - x);
            x = i;
        }
    }

    return rv;
}

- John January 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

will it work for "abc"

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

int main(void) {
char input[]="aaabbcccc";
const int N=sizeof(input)/sizeof(char);
int i;
char *c=input;
char last=0,cnt=0;
for(;;c++){
if(last!=*c){
if(last!=0){
printf("%c%d",last,cnt);
}
last=*c;
cnt=1;
}else cnt++;
if(*c==0) break;
}
return EXIT_SUCCESS;
}

- H January 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void f(char* str) {
int count;
char c;
while (*str != '\0') {
c = *str;
count = 0;
while (*str == c) {
count++;
*str++;
}
cout << c << count;
}
}

- Rocky February 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Rocky: ur code does not deal with single characters so here is a slight modification in ur code (And also the code is in Java and not c)

int count;
char c;
for(int i = 0; i <s.length();){
c = s.charAt(i);
count = 1;
i++;
while(i < s.length() && c == s.charAt(i)){
count++;
i++;
}
System.out.print(c + "" + count);
}

- Anonymous June 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main( )
{
string str("aaabbbbcc");

map<char,int> charCount;

string::iterator it;

for(it = str.begin() ; it != str.end() ; it++)
{
charCount[*it] += 1;
}

map<char,int>::const_iterator itmap;

for(itmap = charCount.begin() ; itmap != charCount.end() ; itmap++)
{
cout << (*itmap).first<< (*itmap).second;
}
}

- sachin February 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Code without using maps
void FormatString(){
string::const_iterator it,it2;
int count = 0;
for(it2 = it = str.begin() ; it != str.end() ; it++)
{
if(*it == *it2){
count++;
}else{

cout << *it2 << count;
it2 = it;
//decreament iterator as it been advanced to next char by for
//loop
it--;
count = 0;
}

}
cout << *it2 << count;
}

- sachin February 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/** Initialize newStr to 2*len(str) **/

int i=0, k=0;
int count=1;

while(str[i] != '\0')
{
	count = 1;
	newStr[k++] = str[i++];
	while(str[i] == newStr[k-1];
	{	
		i++;
		count++;
	}
	newStr[k++] = count;
}
newStr[k] = '\0';

return newStr;

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

How this code will be?
#include<stdio.h>
#include<string.h>

void printfCharCount(char *);

void printfCharCount(char *str)
{
int count=1;
char *ptr=str+1;
int n=strlen(str);
if(n<2)
{
printf("%s%d",str,count);
return;
}
else
{
while(*str!='\0')
{
if(*str==*ptr)
{
while(*str==*ptr)
{
count++;
ptr++;
}
}
printf("%c%d",str[0],count);
if(*str!='\0')
{
str=ptr;
ptr=str+1;
}
count=1;
}
}
}

main()
{
char str[100];
printf("Enter the string\n");
gets(str);
printfCharCount(str);
getch();
}

- KK July 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thank you

- Raghunath March 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public string ReturnCharCount(string input)
{
Hashtable ht = new Hashtable();
StringBuilder sb = new StringBuilder();

if(input.Length == 0)
{
return null;
}

foreach(char ch in input)
{
if (ht.Contains(ch))
{
int val = (int)ht[ch];
ht[ch] = ++val;
}
else
{
ht.Add(ch, 1);
}
}

foreach (Object key in ht.Keys)
{
sb.Append(key.ToString());
sb.Append(ht[key].ToString());
}
return sb.ToString();
}

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

#include<stdio.h>
main()
{
char *str = "abc";
char *p1,*p2,*p3;

int count = 1;


p1 = str;
p2 = str+1;


while(*p1 != '\0')
{
if(count == 1)
{
printf("%c",*p1);
}
if(*p1 == *p2)
{
printf("compare p1 and p2\n");
count++;
}
if(*p1 != *p2)
{
if(count>1)
{
printf("%d",count);
}
count =1;
p1 = p2;
p2 = p2+1;
}
else {
p2++;
}

}
printf("\n");
}

- vtrivedi January 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

{
it would be nice if there is proper indentation
}

- pooja March 18, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Given a string aaaabbccc, convert it to the form a3b2c3
        public static void ConvertToStrings(string str)
        {
            Hashtable ht = new Hashtable();
            for(int i=0;i<str.Length;i++)
            {
                if(ht.ContainsKey(str[i]))
                {
                    int val = Convert.ToInt32(ht[str[i]]);
                    ht[str[i]] = val+1;
                }
                else
                {
                    ht.Add(str[i],1);
                }
            }
            string strnew = string.Empty;
            foreach (var obj in ht.Keys)
            {
                if (ht[obj].ToString().Length != 0)
                {
                   Console.WriteLine(obj.ToString() + "" + ht[obj].ToString());
                }
            }
            Console.WriteLine(strnew);

        }

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

crackprogramming.blogspot.com/2012/10/compress-c-style-string.html

- DelMar October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RepeatedCharAsCount {
	public static void main(String[] args) {
		String str="aaabbbbccccdddddddddddddddd";
		String finalo = new String();
		int count=1,i;
		for(i=0;i<str.length()-1;i++){
			if(str.charAt(i)==str.charAt(i+1))
				count++;
			else{
				finalo=finalo+count+str.charAt(i);
				count=1;
				
			}
		}
		finalo=finalo+count+str.charAt(i);
		System.out.println(finalo);
	}

}

- Sachdefine January 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void HashTableUse(String str){
		LinkedHashMap <Character, Integer> ht=new LinkedHashMap <Character, Integer>();
		String finalo=new String();
		for(int i=0;i<str.length();i++){
			if(ht.containsKey(str.charAt(i))){
				ht.put(str.charAt(i), ht.get(str.charAt(i))+1);
			}else
				ht.put(str.charAt(i), 1);
		}
		System.out.println(ht);
		for(Map.Entry<Character, Integer> entry: ht.entrySet()){
			finalo=finalo+entry.getValue()+entry.getKey();
		}
		System.out.println(finalo);
	}

- Sachdefine January 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

;

- hjvgj March 18, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

{
k}

- hjvgj March 18, 2015 | Flag


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