Interview Question for SDE1s


Country: India




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

Modified to working java code ... still i prefer pseudocode ... :)

public static char[] compress(char[] input){
		int n = input.length;
		if(n <= 1){
			return input;
		}
		//Take two pointers P1 & P2
		//Initialize 
		//	P1 with 0 (position of first character in array) & 
		//	P2 with 1 (position of second character in array)
		int P1 = 0;
		int P2 = 1;
		int count = 1;
		while(P2 < n){			
		    if(input[P1] == input[P2]){
			       count++ ; 
			       P2++;      
				   if(P2 == n) {
				    	 //input[++P1] = (char)(count + "").charAt(0);
				    	 P1 = intToChar(count, input, P1);
				    	 input[++P1]  = input[P2-1];
  //This line is optional, for better output
				    	 input[P1] = '\0';
				    	 return input;
				    }     
		    } else if(input[P1] != input[P2]  && count > 1) {
			         //input[++P1] = (char)(count + "").charAt(0);
		    	     P1 = intToChar(count, input, P1);
				     input[++P1]  = input[P2];
				     count = 0;
		    } else {
		    	     input[++P1]  = input[P2++];
		    }
		}
		return input;
	}
	//Count can be of multiple digits
	private static int intToChar(int count, char[] input, int position){
		String str = count + "";
		for(int i =0; i <str.length(); i++){
			input[++position] = str.charAt(i);
		}
		return position;
	}
	
	public static void main(String[] args) {
		char[] input = {'A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','B','C','C','D','D','D'};
		System.out.println(compress(input));
	}

- Ajeet October 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What for input like ABCD ?

- ninja October 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 3 votes

You didn't define the behavior on the border cases. Why are you asking him?

- American ninja warrior October 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The code is not working since you try to assign int value to array of character

- LinhHA05 October 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

First there is no boundry defined in your question, so i did not consider that.

Second - as per the question, we are going to compress the string. It is a common requirement in business senarios. So for that if size of a character is 1 we did not make any change that.

- Ajeet October 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Fixed ... i missed typecasting of int to char
input[++P1] = (char)count;

- Ajeet October 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Ajeet: I don't think you see the flaws in your code
- (int) 1 # (char) '1'
- (int) 10 -> string '10' and for that P1 need to increase 2

- LinhHA05 October 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

with the assumption that we need to answer an interview question so at least you need to give some kinds of working code. Pseudo code has no longer been accepted in the interview

- LinhHA05 October 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Pseudocode is way better than real code for answering.

- Smerican ninja warrior October 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

This is easy problem however it is the kind of question to see how careful your coding is.
One thing to note is the count need to be converted to string before we write, and the count might have several digits

#include <iostream>
#include <string>
#include <sstream>

using namespace std;
void reptonum(char* s, int n) {
    char *rp, *wp;
    rp = wp = s;

    if (n <= 1) return;

    while (rp < s + n) {
        char cur = *rp;
        int cnt = 0;

        while (*rp == cur && rp < s + n) {
            ++rp;
            ++cnt;
        }

        *wp++ = cur;
        if (cnt > 1) {
            ostringstream convert;
            convert << cnt;
            string nums = convert.str();
            for (int i=0; i< nums.length(); ++i) {
                *wp++ = nums[i];
            }
        }
    }
    *wp = 0;
    cout << s << endl;
}

int main()
{
    char  s[] = "AAAABCDDD";
    reptonum(s, sizeof(s) / sizeof(s[0]));
    char  s1[] = "AAAAAAAAAAAAAAABCCD";
    reptonum(s1, sizeof(s1) / sizeof(s1[0]));
    char  s2[] = "ABCD";
    reptonum(s2, sizeof(s2) / sizeof(s2[0]));
}

- LinhHA05 October 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

int main()
{

char arr[11]="AAAABCCDDD";
int i,index=0,count=0;


for(i=0;i<11;i++)
{
if(arr[index]==arr[i])
{
count++;
}


if(arr[index]!=arr[i] )
{
if(count==1)
{
printf("%c",arr[i-1]);
index=i;
count=0;
--i;
}
else
{
printf("%c%d",arr[i-1],count);
index=i;
count=0;
--i;
}

}

}

}

- vinay October 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be achieved in Java, with simple code like this -

public class AlphabetCount {

	public static void main(String[] args) {
		String str = "AAAABCCDDD";
		//String str = "ABCD";
		//String str = "AAAA";
		//String str = "";
		StringBuffer sb = new StringBuffer(str), sbTarget = new StringBuffer();
		
		char prevChar = ' ', currentChar = ' ';
		int prevCharCount = 0;
		
		for(int i=0, len=sb.length(); i<len; i++)
		{
			currentChar = sb.charAt(i);
			
			if(currentChar != prevChar)
			{
				if(prevCharCount > 1)
					sbTarget.append(prevCharCount);
				
				sbTarget.append(currentChar);
				prevCharCount = 1;
				prevChar = currentChar;
			}
			else
				prevCharCount++;
		}
		
		if(prevCharCount > 1)
			sbTarget.append(prevCharCount);
		
		System.out.println(sbTarget);
	}

}

- ruharish October 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

An efficient solution in c code

#include<stdio.h>
#include<conio.h>
void main()
{
	char a[20];
	static int i,j,n;
	printf("Enter the string:");
	gets(a);
	for(i=0;;)
	{
		for(j=i;a[j]==a[i];j++)
		{
			n++;
		}
		if(n>1)
		printf("%c%d",a[i],n);
		else
		printf("%c",a[i]);
		n=0;
		i=j;
		if(a[i]=='\0')
		break;
	}
	getch();
}

}

- Md Shahjahan October 13, 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