Amazon Interview Question
Software Engineer in TestsUse 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';
}
void f(char* str) {
int count;
char c;
while (*str != '\0') {
c = *str;
count = 0;
while (*str == c) {
count++;
*str++;
}
cout << c << count;
}
}
@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);
}
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;
}
}
//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;
}
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();
}
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();
}
#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");
}
//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);
}
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);
}
}
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);
}
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.
- veerun14 March 17, 2010even 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