Interview Question for Web Developers


Country: United States
Interview Type: Phone Interview




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

Note: assuming we have to count not just consecutive chars. we are counting all chars irrespective of whether they are consecutive.
public static string Compress(string str)
{
string compressed = "";

Dictionary<char, int> sDict = new Dictionary<char, int>();

foreach (char c in str)
{
if (!sDict.ContainsKey(c))
{
sDict.Add(c, 1);
}
else
{
sDict[c]++;
}
}

foreach (KeyValuePair<char, int> keyvalue in sDict)
{
compressed = compressed + keyvalue.Key + keyvalue.Value;
}
return compressed;
}

- gkr July 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Search for run length coding .
Th code is also available on geeksforgeeks

- anaghakr89 July 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class LetterCount {

public static void main(String[] args) {
String str="aaabbba";
compress(str);
}
public static void compress(String str){
int size = countCompression(str);
if(size >str.length()){
return;
}
StringBuffer myStr = new StringBuffer();
char last = str.charAt(0);
int count=1;
for(int i=1;i<str.length();i++){
if(str.charAt(i)==last){
count++;
}else{
myStr.append(last);
myStr.append(count);
last = str.charAt(i);
count=1;
}
}
myStr.append(last);
myStr.append(count);
System.out.println(myStr.toString());
}
public static int countCompression(String str){
char last = str.charAt(0);
int size=0,count=1;
for(int i=1;i<str.length();i++){
if(str.charAt(i)==last){
count++;
}else{
last = str.charAt(i);
size+=1+String.valueOf(count).length();
count=1;
}
}
size+=1+String.valueOf(count).length();
return size;
}
}

- lparka123 July 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def compress(s):
   l = list(s)
   lc = {}
   for i in l:
      lc[i]=0
   for i in l:
       if i in lc:
         lc[i] = lc[i]+1
   sc = ""
   for k in lc:
      sc += str(k)+str(lc[k])
   print sc


if __name__ == "__main__":
   compress("aaabb22erftyggfdsdfsdlfsssop")

- nishant_ki_maa_ka August 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def compress(s):
   l = list(s)
   lc = {}
   for i in l:
      lc[i]=0
   for i in l:
       if i in lc:
         lc[i] = lc[i]+1
   sc = ""
   for k in lc:
      sc += str(k)+str(lc[k])
   print sc


if __name__ == "__main__":
   compress("aaabb22erftyggfdsdfsdlfsssop")

- Sohail August 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution in python using itertools

from itertools import groupby

def compress(a):
     s = ''
     for x in [list(g) for k, g in groupby(a)]:
         s += x[0] + str(len(x))
     return s

- Fernando August 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To @sohail and @nishant_ki_maa_ka those solutions are not doing exactly what the question is asking. The dictionary doesn't remember if two letters were consecutive or not
for the given example it will return "a4b3" which is incorrect

- Fernando August 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*Compress String with character and its count.
Example: "aaabbba" -> compress -> "a3b3a1”*/

public class Primero_ContarCantidadDeLetrasEnString {
public static void main(String[] args) {

String word = "aaaabccdddef";
System.out.println(" Original Word: " + word + " compressed word " + compressString(word));

String word2 = "abbcccddddeeeeefffff";
System.out.println(" Original Word: " + word2 + " compressed word " + compressString(word2));

String word3 = "ana";
System.out.println(" Original Word: " + word3 + " compressed word " + compressString(word3));


}

private static String compressString(String word) {


char[] a = word.toCharArray();
StringBuilder endWord = new StringBuilder();
int count = 1;

System.out.println(a.length);
for (int i = 0; i < a.length; i++) {
if (i != a.length - 1) {
if (a[i] == a[i + 1]) {

count++;

} else {

if (count > 1) {

endWord.append(String.valueOf(count)).append(String.valueOf(a[i]));
count = 1;
} else {

endWord.append(String.valueOf(a[i]));
count = 1;
}

}
} else {

if (a[i] == a[i - 1]) {
count++;
endWord.append(String.valueOf(count)).append(String.valueOf(a[i]));
} else
endWord.append(String.valueOf(a[i]));

}
}
System.out.println(endWord);
return endWord.toString();
}
}

- Belen August 02, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*Compress String with character and its count. 
Example: "aaabbba" -> compress -> "a3b3a1”*/ 

public class Primero_ContarCantidadDeLetrasEnString {
	public static void main(String[] args) {

		String word = "aaaabccdddef";
		System.out.println(" Original Word: " + word + " compressed word " + compressString(word));
		
		String word2 = "abbcccddddeeeeefffff";
		System.out.println(" Original Word: " + word2 + " compressed word " + compressString(word2));
		
		String word3 = "ana";
		System.out.println(" Original Word: " + word3 + " compressed word " + compressString(word3));
		

	}

	private static String compressString(String word) {
		

		char[] a = word.toCharArray();
		StringBuilder endWord = new StringBuilder();
		int count = 1;

		System.out.println(a.length);
		for (int i = 0; i < a.length; i++) {
			if (i != a.length - 1) {
				if (a[i] == a[i + 1]) {

					count++;

				} else {

					if (count > 1) {

						endWord.append(String.valueOf(count)).append(String.valueOf(a[i]));
						count = 1;
					} else {

						endWord.append(String.valueOf(a[i]));
						count = 1;
					}

				}
			} else {

				if (a[i] == a[i - 1]) {
					count++;
					endWord.append(String.valueOf(count)).append(String.valueOf(a[i]));
				} else
					endWord.append(String.valueOf(a[i]));

			}
		}
		System.out.println(endWord);
		return endWord.toString();
	}
}

- Belen August 02, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*Compress String with character and its count. 
Example: "aaabbba" -> compress -> "a3b3a1”*/ 

public class Primero_ContarCantidadDeLetrasEnString {
	public static void main(String[] args) {

		String word = "aaaabccdddef";
		System.out.println(" Original Word: " + word + " compressed word " + compressString(word));
		
		String word2 = "abbcccddddeeeeefffff";
		System.out.println(" Original Word: " + word2 + " compressed word " + compressString(word2));
		
		String word3 = "ana";
		System.out.println(" Original Word: " + word3 + " compressed word " + compressString(word3));
		
	}

	private static String compressString(String word) {
		
		StringBuilder finalWord = new StringBuilder();
		int count = 1;

		System.out.println(word.length());
		for (int i = 0; i < word.length(); i++) {
			if (i != word.length() - 1) {
				if (word.charAt(i) == word.charAt(i + 1)) {

					count++;

				} else {

					if (count > 1) {

						appendRepetedCharacter(word, finalWord, count, i);
						count = 1;
					} else {

						appendSingleCharacter(word, finalWord, i);
						count = 1;
					}

				}
			} else {

				if (word.charAt(i) == word.charAt(i-1)) {
					count++;
					appendRepetedCharacter(word, finalWord, count, i);
				} else
					appendSingleCharacter(word, finalWord, i);

			}
		}
		System.out.println(finalWord);
		return finalWord.toString();
	}

	private static void appendSingleCharacter(String word, StringBuilder finalWord, int i) {
		finalWord.append(String.valueOf(word.charAt(i)));
	}

	private static void appendRepetedCharacter(String word, StringBuilder finalWord, int count, int i) {
		finalWord.append(String.valueOf(count)).append(String.valueOf(word.charAt(i)));
	}
}

- Belen August 02, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String check(String input) {
		Stack<Character> temp = new Stack<>();
		StringBuilder output = new StringBuilder();
		for (int i = 0; i < input.length(); i++) {

			if (temp.isEmpty())
				temp.push(input.charAt(i));
			else if (i == (input.length() - 1) && !temp.isEmpty()) {
				if (temp.peek() != input.charAt(i)) {
					output.append(temp.peek()).append(Integer.toString(temp.size()));
					output.append(input.charAt(i)).append(1);
				} else
					output.append(temp.peek()).append(Integer.toString(temp.size() + 1));
			} else if (temp.peek() != input.charAt(i)) {
				output.append(temp.peek()).append(Integer.toString(temp.size()));
				temp.removeAllElements();
				temp.push(input.charAt(i));
			} else
				temp.push(input.charAt(i));
		}

		return output.toString();

}

- Shankha Mandal August 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String check(String input) {
Stack<Character> temp = new Stack<>();
StringBuilder output = new StringBuilder();
for (int i = 0; i < input.length(); i++) {

if (temp.isEmpty())
temp.push(input.charAt(i));
else if (i == (input.length() - 1) && !temp.isEmpty()) {
if (temp.peek() != input.charAt(i)) {
output.append(temp.peek()).append(Integer.toString(temp.size()));
output.append(input.charAt(i)).append(1);
} else
output.append(temp.peek()).append(Integer.toString(temp.size() + 1));
} else if (temp.peek() != input.charAt(i)) {
output.append(temp.peek()).append(Integer.toString(temp.size()));
temp.removeAllElements();
temp.push(input.charAt(i));
} else
temp.push(input.charAt(i));
}

return output.toString();
}

- Shankha Mandal August 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String check(String input) {
		Stack<Character> temp = new Stack<>();
		StringBuilder output = new StringBuilder();
		for (int i = 0; i < input.length(); i++) {

			if (temp.isEmpty())
				temp.push(input.charAt(i));
			else if (i == (input.length() - 1) && !temp.isEmpty()) {
				if (temp.peek() != input.charAt(i)) {
					output.append(temp.peek()).append(Integer.toString(temp.size()));
					output.append(input.charAt(i)).append(1);
				} else
					output.append(temp.peek()).append(Integer.toString(temp.size() + 1));
			} else if (temp.peek() != input.charAt(i)) {
				output.append(temp.peek()).append(Integer.toString(temp.size()));
				temp.removeAllElements();
				temp.push(input.charAt(i));
			} else
				temp.push(input.charAt(i));
		}

		return output.toString();
	}

- Shankha Mandal August 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String check(String input) {
		Stack<Character> temp = new Stack<>();
		StringBuilder output = new StringBuilder();
		for (int i = 0; i < input.length(); i++) {

			if (temp.isEmpty())
				temp.push(input.charAt(i));
			else if (i == (input.length() - 1) && !temp.isEmpty()) {
				if (temp.peek() != input.charAt(i)) {
					output.append(temp.peek()).append(Integer.toString(temp.size()));
					output.append(input.charAt(i)).append(1);
				} else
					output.append(temp.peek()).append(Integer.toString(temp.size() + 1));
			} else if (temp.peek() != input.charAt(i)) {
				output.append(temp.peek()).append(Integer.toString(temp.size()));
				temp.removeAllElements();
				temp.push(input.charAt(i));
			} else
				temp.push(input.charAt(i));
		}

		return output.toString();
	}

- superdudesan August 04, 2017 | 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