Interview Question


Country: United States




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

{{
public String processedString(String input) {
if (input == null || input == "") return null;

String output = "";
int counter = 1;
char prevLetter = input.charAt(0);
char curLetter = 0;

for(int i = 1; i < input.length(); i++) {
curLetter = input.charAt(i);
if (curLetter == prevLetter) {
counter++;
} else {
output += prevLetter;
output += String.valueOf(counter);

prevLetter = curLetter;
counter = 1;
}
}

// last letter
output += curLetter;
output += String.valueOf(counter);

return output;
}
}}

- Gee March 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

try doing this inplace, more challenging...!!

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

public static char[] convert(String s){
		char[] temp = s.toCharArray() ; 
		
		int len = temp.length ; 
		char [] rel = new char[len]; 
		int i = 0, k =0 ; 
		while( i <= len -1  && k <= len -1  ){
			rel[k] = temp[i]; 
			int count = 1 ; 
			while( i <= temp.length- 2 && temp[i] == temp[i+1] ){
				count ++ ; 
				i = i +1; 
			}
			if( count > 1 ){
				rel[k+1] = Character.toChars(count + '0')[0];
				k = k +2 ; 
			}else k = k + 1; 
			
			i = i + 1; 
			System.out.println(" i : " + i  + " k : " + k );
		}
		return rel ; 
	}

- KHUE0011 March 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.*;
public class Counts
{
public static void main(String[] args) throws IOException
{
InputStreamReader obj = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(obj);
System.out.println("Enter the string:");
String str = br.readLine();
int size = str.length();
char c[] = new char[size];
str.getChars(0,size,c,0);
//creating an integer array to store the values of the characters of the string array
int b[] = new int[size];
for(int i=0;i<size;i++)
{
int temp = (int)c[i];
b[i]=temp;
}
//now let us sort the array of integers using bubble sort
for(int i=0;i<size;i++)
{
for(int j=0;j+1<size;j++)
{
if(b[j+1]<b[j])
{
//do swapping
b[j] = b[j] + b[j+1];
b[j+1] = b[j] - b[j+1];
b[j] = b[j]-b[j+1];
}
}
}
//now take the contents of the values of the integer array and store them in the char array
System.out.println("Array after sorting");
for(int i=0;i<size;i++)
{
char cc = (char)b[i];
c[i]=cc;
System.out.println(c[i]);
}
//now let us count the frequency of each of the characters
System.out.println("--------------------------");
int length=0,m=0;
for(int i=0;i<size;i++)
{
char key = c[m];
System.out.print(key);
for(int j=m;j<size;j++)
{
if(c[j]==key)
{
length++;
}
else
{
break;
}
}
if(length==1)
{
System.out.print("");
}
else
{
System.out.print(length);
}
m += length;
length=0;
if(m==size)
break;
}

}
}

- sourav March 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.*;
public class Counts
{
public static void main(String[] args) throws IOException
{
InputStreamReader obj = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(obj);
System.out.println("Enter the string:");
String str = br.readLine();
int size = str.length();
char c[] = new char[size];
str.getChars(0,size,c,0);
//creating an integer array to store the values of the characters of the string array
int b[] = new int[size];
for(int i=0;i<size;i++)
{
int temp = (int)c[i];
b[i]=temp;
}
//now let us sort the array of integers using bubble sort
for(int i=0;i<size;i++)
{
for(int j=0;j+1<size;j++)
{
if(b[j+1]<b[j])
{
//do swapping
b[j] = b[j] + b[j+1];
b[j+1] = b[j] - b[j+1];
b[j] = b[j]-b[j+1];
}
}
}
//now take the contents of the values of the integer array and store them in the char array
System.out.println("Array after sorting");
for(int i=0;i<size;i++)
{
char cc = (char)b[i];
c[i]=cc;
System.out.println(c[i]);
}
//now let us count the frequency of each of the characters
System.out.println("--------------------------");
int length=0,m=0;
for(int i=0;i<size;i++)
{
char key = c[m];
System.out.print(key);
for(int j=m;j<size;j++)
{
if(c[j]==key)
{
length++;
}
else
{
break;
}
}
if(length==1)
{
System.out.print("");
}
else
{
System.out.print(length);
}
m += length;
length=0;
if(m==size)
break;
}

}
}

- sourav March 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public String compressString(String s) {
	
	
	String out;
	int currentCounter = 1;

	for (int i = 1; i < s.length; i++ {

		while (i < s.length && s.charAt(i) == s.charAt(i - 1)) {

			currentCounter++;
			i++;
		}

		out += s.charAt(i-1) + currentCounter; 
		currentCounter = 0;
	}

	return out;
}

- SK March 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public String compress(String target){
		char [] chs = target.toCharArray() ;
		int tail = 0 , j = 0 ;
		for (int i = 1 ; i <= chs.length ; ++i) {
			if (i == chs.length || chs[j] != chs[i]) {
				chs[tail++] = chs[j] ;
			       if (i - j > 1) {
			    	   if (i - j < 10) {
			    		   chs[tail] = (char)((i - j) + '0');
				    	   tail++;   
			    	   } else{
			    		   char [] digits = String.valueOf(i - j).toCharArray() ;
			    		   for (char c : digits) {
			    			   chs[tail++] = c ;
			    		   }
			    	   }			    	   
			       }		       		       
			       j = i ;
			} 
		}				
		return new String(chs, 0, tail) ;

}

- Scott March 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I know the question to code in Java, I am providing a Python answer:

>>> import re
>>> def squeeze(astring):
...     alist = re.findall(r'(\w)(\1*)', astring)
...     return ''.join([i+str(len(j)+1) if j else i for i,j in alist])
... 
>>> squeeze('aaahhrrrvfuuk')
'a3h2r3vfu2k'

- Adhitya March 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.LinkedHashMap;
import java.util.Map;

public class MyMap {

	public static void main(String[] args) {
		String s="aaahhrrrvfuuk";
		Map<Character,Integer> m=new LinkedHashMap<Character,Integer>();

		int value=0;
		for(int i=0;i<s.length();i++){
			if(m.containsKey(s.charAt(i))){
				value=Integer.parseInt((m.get(s.charAt(i))).toString());
				m.put(s.charAt(i), value+1);
			}
			else
				m.put(s.charAt(i), 1);
		}	
		for (Map.Entry entry : m.entrySet()) {
			String key = entry.getKey().toString();;
			Integer val =Integer.parseInt(entry.getValue().toString());
			if(val==1)
				System.out.print(key);
			else
				System.out.print(key + "" + val);
		}
	}

}

- Debajyoti Kundu March 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.LinkedHashMap;
import java.util.Map;

public class MyMap {

	public static void main(String[] args) {
		String s="aaahhrrrvfuuk";
		Map<Character,Integer> m=new LinkedHashMap<Character,Integer>();

		int value=0;
		for(int i=0;i<s.length();i++){
			if(m.containsKey(s.charAt(i))){
				value=Integer.parseInt((m.get(s.charAt(i))).toString());
				m.put(s.charAt(i), value+1);
			}
			else
				m.put(s.charAt(i), 1);
		}	
		for (Map.Entry entry : m.entrySet()) {
			String key = entry.getKey().toString();;
			Integer val =Integer.parseInt(entry.getValue().toString());
			if(val==1)
				System.out.print(key);
			else
				System.out.print(key + "" + val);
		}
	}

}

- Debajyoti Kundu March 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
int main()
{
char str[100001];
int i,cnt=0;
cin>>str;
int len=strlen(str);
for(i=0;i<len;i++)
{

if(str[i]==str[i+1])
{
cnt++;
}
else
{
cout<<str[i]<<cnt+1;
cnt=0;
}

}
return 0;
}

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

Java solution:

public static String compact(String s){
	int n = s.length();
    StringBuilder builder = new StringBuilder();
    
    int idx = 0;
    
    while (idx < n){
    	char c = s.charAt(idx);
        int count = 0;
        
        while (idx < n && s.charAt(idx) == c){
        	count++;
            idx++;
        }
        
        builder.append(c);
        
        if (count > 1){
        	builder.append(count);
        }
    }
    
    return builder.toString();
}

- Inucoder March 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StrCount{
   public static void main(String []args){
       String str="aaawwrrrvfuuk";
	   String str2="";
	   int n=0;
	   for(int i=0;i<str.length();i++){
	      char c=str.charAt(i);
		  if(i==0){
		    str2=""+c;
		  } 
		  if(c==str2.charAt(str2.length()-1)){
		     n++;
		  }
		  else{
		      if(n>1)
			    str2=str2+n;
			  str2=str2+c;	
			  n=1;
		  }
	   }
	   System.out.println(str2);
   }
}

- sadhanaj28 March 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class sample{
public static void main(String[] args) {
String input = "aaahhrrrvfuuk";
char prevChar = '\0';
int count = 1;
for (int i = 0; i < input.length(); i++) {
if (i == 0) {
System.out.print(input.charAt(i));
} else {
if (input.charAt(i) == prevChar) {
count++;
} else {
if (count > 0) {
System.out.print(count);
count = 0;
}
System.out.print(input.charAt(i));
}
}
prevChar = input.charAt(i);
}
}
}

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

public class Collection1 {
public static void main(String[] args) {
String input = "aaahhrrrvfuuk";
char prevChar = '\0';
int count = 1;
for (int i = 0; i < input.length(); i++) {
if (i == 0) {
System.out.print(input.charAt(i));
} else {
if (input.charAt(i) == prevChar) {
count++;
} else {
if (count > 0) {
System.out.print(count);
count = 0;
}
System.out.print(input.charAt(i));
}
}
prevChar = input.charAt(i);
}
}
}

- swati March 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		String s = "aaahhrrrvfuuk";
		Stack<String> st = new Stack<String>();
		int cnt = 1;
		st.push(s.charAt(0)+"");
		for (int i = 1; i < s.length(); i++) {
			String c = s.charAt(i)+"";
			if(!st.peek().equals(c)) {
				if(cnt > 1) {
					st.push(String.valueOf(cnt));
					cnt = 1;
				}
				st.push(c);
			}
			else {
				cnt++;
			}
		}
		StringBuilder res = new StringBuilder();
		for (String string : st) {
			res.append(string);
		}
		System.out.println(res);
	}

- xiang April 14, 2015 | 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