Amazon Interview Question for Software Engineer / Developers






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

#include<iostream>
#include<string>

using namespace std;

char * rle(char str[])
{
    int i=1, count =1;
    int k=0;
    char *a= new char(strlen(str));
    a[0]=str[0];
    while(str[i]!='\0')
    {
        if(str[i]==str[i-1])
            count++;
        else
        {
            if(count>1)
                a[++k]=count+'0';
            a[++k]=str[i];
            count=1;
        }
        i++;
    }
    if(count>1)
        a[++k]=count+'0';
    a[++k]='\0';
    return a;
}

int main()
{
    char str[]="aaabbbbcdd";
    cout<<rle(str);
    return 0;
}

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

@sonal....
your code will not work for the strings which contains continuous run length of a character > 9...like aaaaaaaaaa,abbbbbbbbbbbc etc....

you should consider these case as well..

you can use ..itoa(count) and append to string a.

- omprakpal July 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@omprakpal: ya...thanks alot!!

- Sonal July 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

More over output string length sud be 2n.
If all the elements are different then you need it 2n
as like
abcdef so output will be a1b1c1d1e1f1

- ashish July 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

run length encoding :)
0(n) time algorithm :)
void runlengthencoding(char s[],char run[])
{
int n,k=0;
int count;
int i,j;
k=0;
n=strlen(s);
for(i=0;i<=n-1;)
{
run[k]=s[i];
count=1;
for(j=i+1;j<=n-1;j++)
{
if(s[j]!=s[i])
break;
else
count++;
}
i=j;
sprintf(run+k+1,"%d",count);
k=k+1;
while(count!=0)
{
count=count/10;
k++;
}

}
run[k]='\0';

}

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

public class CompressString
{	
	public static void main(String[] args)
	{  
		System.out.println("[CompressString] in main()");
	
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter the string:");
		try {
			String str= br.readLine();
			StringBuffer strBuff=new StringBuffer();
			strBuff.append(str.charAt(0));
			int len=str.length();
			for(int i=1;i<len;)
			{
				char ch=str.charAt(i);
				char chBuff=strBuff.charAt(strBuff.length()-1);
			
				if(ch==chBuff)
				{  
					int num=1;
					strBuff.append(num);
				
					while(ch==chBuff )
					{						
						num++;
						strBuff.deleteCharAt(strBuff.length()-1);
						strBuff.append(num);
						i++;
						if(i<len)
						ch=str.charAt(i);
						else 
							break;						
					}					
				}
				else
				{
					strBuff.append(ch);
					i++;
				}
			}
			
			System.out.println("Output String :   "+strBuff);
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

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

public class CompressString
{	
	public static void main(String[] args)
	{  
		System.out.println("[CompressString] in main()");
	
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter the string:");
		try {
			String str= br.readLine();
			StringBuffer strBuff=new StringBuffer();
			strBuff.append(str.charAt(0));
			int len=str.length();
			for(int i=1;i<len;)
			{
				char ch=str.charAt(i);
				char chBuff=strBuff.charAt(strBuff.length()-1);
			
				if(ch==chBuff)
				{  
					int num=1;
					strBuff.append(num);
				
					while(ch==chBuff )
					{						
						num++;
						strBuff.deleteCharAt(strBuff.length()-1);
						strBuff.append(num);
						i++;
						if(i<len)
						ch=str.charAt(i);
						else 
							break;						
					}					
				}
				else
				{
					strBuff.append(ch);
					i++;
				}
			}
			
			System.out.println("Output String :   "+strBuff);
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

- chandanbaranwal August 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void RunLengh(string s1)
{
char[] arr = s1.ToArray();
string result = string.Empty;
int count = 1;

for (int i = 0; i < s1.Length; i++)
{
if (result.EndsWith(arr[i].ToString()) == true)
count = count + 1;
else if (count > 1)
{
result = result + count.ToString();
count = 1;
result = result + arr[i];
}
else
result = result + arr[i];
}

if (count > 1)
result = result + count.ToString();

Console.WriteLine(result);
}

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

string encodeRLE(const string &src)
{
size_t len = src.length();
ostringstream os;
for(size_t i = 0; i < len; ) {
size_t j;
int count = 1;
for(j = i + 1; j < len; j++) {
if(src[i] == src[j])
++count;
else
break;
}
if(count != 1)
os << src[i] << count;
else
os << src[i];
i = j;
}
return os.str();
}

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

private static string consecutiveRepeatCount(char[] arr)
{
string result = "";
int count = 0, max = 0;
int num = arr[0];

for (int i = 0; i < arr.Length - 1; i++)
{
if (arr[i] == arr[i + 1])
{
count++;
if (i + 1 == arr.Length - 1 ) // for last count match
{
result += arr[i];
if (count != 0)
{
int n = count + 1;
result += n;
}
}
}
else
{
result += arr[i];
if (count != 0)
{
int n= count+1;
result += n;
}
if (count > max)
{
num = arr[i];
max = count;
}
if (i + 1 == arr.Length - 1) // for last count print
{
result += arr[i+1];
}
count = 0;
}


}
return result;

}

- Pawan January 30, 2012 | 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