Amazon Interview Question for Testing / Quality Assurances


Country: India
Interview Type: Written Test




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

Easy as below in C#

public void FindConsecutiveRepetitiveWithNoDict(string input)
        {
            var previous = '-';
            int sum = 0;
            var output = 0;
            var caught = '*';
            foreach (char a in input)
            {
                if (previous == a)
                {
                    if (sum < ++output)
                    {
                        sum = output;
                        caught = a;
                    }
                }
                else
                {
                    output = 1;
                }
                previous = a;
            }
            Console.WriteLine("{0} -> {1}", caught,sum);

}

- maksymas March 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// ZoomBA 
word = "ffgggtvshjsdhjfffffffhvjbjcharu" 
max = { 'count' : 0, 'letter' : null , 'current' : 0 }
reduce ( word.value ) -> {
  continue ( $.o == $.p ){ max.current += 1 ; $.o }
  if ( max.count < max.current ){
    max.count = max.current
    max.letter = $.p 
    max.current = 1
  }
  $.o 
}
max -= 'current'
println( max )

- NoOne March 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- (void) maxRepitiveChar: (NSString*)string{
	if([string length]==0){
		NSLog(@“nil string”);
		return;
	}
	else if([string length]==1){
		NSLog(@“%@—>1”,string);
		return;
	}
	else{
		char maxChar;
		int maxCount;
		char tempChar;
		int tempCount;
		for(int i = 0; i < [string length]; i++){
			char temp = [string charAtIndex: I];
			if(temp == tempChar){
				tempCount++;
				if(tempCount > maxCount){
					maxChar = tempChar;
					maxCount = tempCount;
				}
			}
			else{
				tempChar = temp;
				tempCount = 1;
			}	
		}
		NSLog(@“%c —> %d”,maxChar, maxCount);
	}
}

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

import java.util.Scanner;

public class MostRepetitiveChar {

	public static void mostRepetitiveChar(String input){
		char c ='\0';
		char previous = '\0';
		char result = '\0';
		int most = 0;
		int count = 0;
		
		for(int i = 0; i < input.length(); i++)
		{
			c = input.charAt(i);
			if(c == previous)
			{
				count++;
				
				if(most < count)
				{
					most = count;
					result = c;
				}		
			}
			else
			{
				count = 1;
			}
			
		if(most < count)
			{
				most = count;
				result = c;
			}
			
			previous = c;
					
		}
		System.out.println(result + " " + most);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		
		String input = in.nextLine();
		
		mostRepetitiveChar(input);
		
		in.close();
		
	}

}

- ank.bst March 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String a="ffgggggtvshjsdhjfffffffhvjbjcharu";
		int max=0, sum=0;
		char prev=a.charAt(0);
		for (int i = 1; i < a.length(); i++) {
			
			if(a.charAt(i)==prev) sum++;
			
			else{
				max=Math.max(sum, max);
				sum=0;			
				}
			
			prev=a.charAt(i);
		}
		System.out.println(max+1);

- sm.khan2010 March 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static int GetLongestConceqChar(string str, out char? lcChar)
{
lcChar = null;
int lcLen = 0;
if (string.IsNullOrEmpty(str))
{
return lcLen;
}

lcLen = 1;
lcChar = str[0];

int tlcLen = 0;
char? tlcChar = null;
for (int i = 1; i < str.Length; i++)
{
char c = str[i];
if (c == tlcChar)
{
tlcChar = c;
tlcLen++;
}
else
{
tlcChar = c;
tlcLen = 1;
}

if (lcLen < tlcLen)
{
lcLen = tlcLen;
lcChar = tlcChar;
}
}

return lcLen;
}

- fa1987 March 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int maxConsecutiveLetters(String str){
		if(str== null || str.isEmpty())return 0;
		int maxCount = 0;
		int currentCount = 1;
		int previousChar = str.charAt(0);
		run : for(int index = 1; index < str.length(); index++){
			if(str.charAt(index) == previousChar){
				currentCount++;
				maxCount = maxCount>currentCount ? maxCount : currentCount;
				continue run;
			}else{
				currentCount = 1;
				previousChar = str.charAt(index);
			}
		}
		return maxCount;

}

- Goutham March 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static string CountMostRepeatedCharacters(string s)
        {

            var chars = s.ToCharArray();

            var currentChar=chars[0];
            var currentCount=1;

            var count = 1;
            int i;
          
            for (i = 1; i < s.Length; i++)
            {
                if (chars[i]==chars[i-1])
                {
                    count++;
                }
                else
                {
                    if (count > currentCount)
                    {
                        currentChar = chars[i - 1];
                        currentCount = count;
                    }
                    count = 1;

                }
            }

            if (count > currentCount)
            {
                currentChar = chars[i - 1];
                currentCount = count;
            }
            return currentChar.ToString() + " " + currentCount.ToString();
        }

- sunil.sebastian March 16, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reapitedMaxChars(String str){


char maxchar=str.charAt(0);
int maxcount=1;

for (int i = 0; i < str.length(); i++) {

char tmp=str.charAt(i);
int count=1;
for (int j = i+1; j < str.length(); j++) {
if(tmp == str.charAt(j)){
count++;
}
else{
if(maxchar == tmp){
if(maxcount < count){
maxcount=count;
}
}
else{
if(maxcount < count){
maxchar=tmp;
}
}
i=j;
break;
}
}

}

System.out.println(maxchar +" -> "+maxcount);

}

- vijay lokhande March 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reapitedMaxChars(String str){


char maxchar=str.charAt(0);
int maxcount=1;

for (int i = 0; i < str.length(); i++) {

char tmp=str.charAt(i);
int count=1;
for (int j = i+1; j < str.length(); j++) {
if(tmp == str.charAt(j)){
count++;
}
else{
if(maxchar == tmp){
if(maxcount < count){
maxcount=count;
}
}
else{
if(maxcount < count){
maxchar=tmp;
}
}
i=j;
break;
}
}

}

System.out.println(maxchar +" -> "+maxcount);

}

- vijay.lokhande.in March 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A = "kslajdfiouewqrqopru"
thechar = None
maxsum = 0
sum = 0
c = A[0]
for d in A[1:]:
if d == c:
sum += 1
chars = d
if sum > maxsum:
maxsum = sum
thechar = d
else:
c = d
sum = 0
if thechar != None:
print("the char is :"+thechar)
print(maxsum+1)
else:
print("No consecutive chars")

- Nagashree March 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#python
A = "kslajdfiouewqrqopru"
thechar = None
maxsum = 0
sum = 0
c = A[0]
for d in A[1:]:
    if d == c:
        sum += 1
        chars = d
        if sum > maxsum:
            maxsum = sum
            thechar = d
    else:
        c = d
        sum = 0
if thechar != None:
    print("the char is :"+thechar)
    print(maxsum+1)
else:
    print("No consecutive chars")

- Nagashree March 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#python
A = "ffgggtvshjsdhjfffffffhvjbjcharu"
thechar = None
maxsum = 0
sum = 0
c = A[0]
for d in A[1:]:
    if d == c:
        sum += 1
        chars = d
        if sum > maxsum:
            maxsum = sum
            thechar = d
    else:
        c = d
        sum = 0
if thechar != None:
    print("the char is :"+thechar)
    print(maxsum+1)
else:
    print("No consecutive chars")

- Nagashree March 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A = "ffgggtvshjsdhjfffffffhvjbjcharu"
thechar = None
maxsum = 0
sum = 0
c = A[0]
for d in A[1:]:
if d == c:
sum += 1
chars = d
if sum > maxsum:
maxsum = sum
thechar = d
else:
c = d
sum = 0
if thechar != None:
print("the char is :"+thechar)
print(maxsum+1)
else:
print("No consecutive chars")

- Nagashree March 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
//char *s="aaaddddddddccffbbbbbbbbbbbeeeeeeee";
char *s="aaaddddddddccffbbbbbbbbbbbeeeeeeee";
int current_count=0,previous_count=0;
char *p=&s[0];
for(int i=0;i<=strlen(s);i++)
{
 if( *p==s[i])
  {
   /* printf("Character is %c at poistion %d is:",*p,i); */
   current_count++;
   previous_count= previous_count > current_count ?
previous_count:current_count;
   }
 else
  {
   p=&s[i];
   current_count=1;
  }
 }
 printf("Frequent Count is %d\n",previous_count);
 return 0;
 }

/* output: Frequent Count is 11 */

- srinu March 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
//char *s="aaaddddddddccffbbbbbbbbbbbeeeeeeee";
char *s="aaaddddddddccffbbbbbbbbbbbeeeeeeee";
int current_count=0,previous_count=0;
char *p=&s[0];
for(int i=0;i<=strlen(s);i++)
{
 if( *p==s[i])
  {
   /* printf("Character is %c at poistion %d is:",*p,i); */
   current_count++;
   previous_count= previous_count > current_count ?
previous_count:current_count;
   }
 else
  {
   p=&s[i];
   current_count=1;
  }
 }
 printf("Frequent Count is %d\n",previous_count);
 return 0;
 }

/* output: Frequent Count is 11 */

- srinu.bg March 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple and easy code

#include<bits/stdc++.h>
using namespace std;
int main()
{
string str;
cin>>str;
char sym;
int ans=0,res=0;
for(int i=0;i<str.length();i++)
{
while(str[i]==str[i+1])
{
ans++;
i++;}
sym=str[i];
res=max(res,ans);
ans=0;
}
cout<<sym<<"  "<<res+1<<endl;

}

- rahul_kumar March 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// C/C++ program to segregate even and odd nodes in a
// Linked List
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char a[] ="ffgggtvshjsdhjfffffffhvjbjcharu";
int i,j,k;
int max_count=0;
int count= 0;
i = strlen(a);


for(j=0;j<i;j++)
{ k=j;
count = 1;
while(a[k] == a[k+1])
{ count++;
k++;
}

if(count > max_count) max_count = count;
}

printf("%d",max_count);
}

- Ajay Bhatia March 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// C/C++ program to segregate even and odd nodes in a
// Linked List
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char a[] ="ffgggtvshjsdhjfffffffhvjbjcharu";
int i,j,k;
int max_count=0;
int count= 0;
i = strlen(a);


for(j=0;j<i;j++)
{ k=j;
count = 1;
while(a[k] == a[k+1])
{ count++;
k++;
}

if(count > max_count) max_count = count;
}

printf("%d",max_count);
}

- ajkool111 March 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void mostFrequentCharacter(String str)
    {
        char prevChar='*',nextChar=str.charAt(0);
        int prevCount=1, nextCount=1;

        for(int i=1; i<str.length(); i++)
        {
            if(str.charAt(i) == nextChar) {
                nextCount += 1;
            }
            else {
                if(prevCount < nextCount)
                {
                    prevChar = nextChar;
                    prevCount = nextCount;
                }
                nextChar = str.charAt(i);
                nextCount = 1;
            }
        }
        if(prevCount > nextCount)
            System.out.println(prevChar);
        else System.out.println(nextChar);

    }

- Sumita March 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void mostFrequentCharacter(String str)
    {
        char prevChar='*',nextChar=str.charAt(0);
        int prevCount=1, nextCount=1;

        for(int i=1; i<str.length(); i++)
        {
            if(str.charAt(i) == nextChar) {
                nextCount += 1;
            }
            else {
                if(prevCount < nextCount)
                {
                    prevChar = nextChar;
                    prevCount = nextCount;
                }
                nextChar = str.charAt(i);
                nextCount = 1;
            }
        }
        if(prevCount > nextCount)
            System.out.println(prevChar);
        else System.out.println(nextChar);

    }

- sumita March 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.thread;

public class string {
public static void main(String args[]){
String str="adddfffffffffffffccrryyygggggghmmmmmmmmmmmmmmmmmmmmmj";
String c = "";
String d="";
boolean counter=false;
for(int i=0;i<str.length()-1;i++){

if(str.charAt(i)==str.charAt(i+1)){
if(c!=null && c.length()>0 && c.charAt(0)==str.charAt(i))
c+=String.valueOf(str.charAt(i));

else if(c!=null && c.length()==0)
c=String.valueOf(str.charAt(i));
else
d+=Character.toString(str.charAt(i));
}
else
counter=true;
if(counter)
{
counter=false;
if(c!=null && d!=null && c.length()>0 && d.length()>0){
if (c.length()>d.length())
d="";
else{
c=d;
d="";
}
}
//c=str.charAt(i);
}
}
System.out.println(c);
System.out.println(d);
}
}

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

package com.thread;

public class string {
public static void main(String args[]){
String str="adddfffffffffffffccrryyygggggghmmmmmmmmmmmmmmmmmmmmmj";
String c = "";
String d="";
boolean counter=false;
for(int i=0;i<str.length()-1;i++){

if(str.charAt(i)==str.charAt(i+1)){
if(c!=null && c.length()>0 && c.charAt(0)==str.charAt(i))
c+=String.valueOf(str.charAt(i));

else if(c!=null && c.length()==0)
c=String.valueOf(str.charAt(i));
else
d+=Character.toString(str.charAt(i));
}
else
counter=true;
if(counter)
{
counter=false;
if(c!=null && d!=null && c.length()>0 && d.length()>0){
if (c.length()>d.length())
d="";
else{
c=d;
d="";
}
}
//c=str.charAt(i);
}
}
System.out.println(c);
System.out.println(d);
}
}

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

public class TextOcuurence {

static int max=0;
//Time Complexity O(logn)
//find
public static int searchFirstOccurrence(char [] a, char target) {

return String.copyValueOf(a).indexOf(target);

}

//Time Complexity O(logn)
public static int searchLastOccurrence(char [] a, char target) {


return String.copyValueOf(a).lastIndexOf(target);
}

//Time Complexity O(logn)
public static int countOccurrence(String a) {
char [] st = a.toCharArray();
java.util.Arrays.sort(st);
int tempsum = 0;
for(int i=0;i<st.length;i++){
tempsum = searchLastOccurrence(st, st[i]) - searchFirstOccurrence(st,st[i]) + 1;
if (max < tempsum)
max=tempsum;
}
return max;
}

public static void main(String[] args) {
String s = "ffgggtvshjsdhjfffffffhvjbjcharu";
System.out.println(countOccurrence(s));
}

}

- reda April 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class TextOcuurence {

    static int max=0;
    //Time Complexity O(logn)
    //find 
    public static int searchFirstOccurrence(char [] a, char target) {
    	
        return String.copyValueOf(a).indexOf(target);

    }

    //Time Complexity O(logn)
    public static int searchLastOccurrence(char [] a, char target) {


        return String.copyValueOf(a).lastIndexOf(target);
    }

    //Time Complexity O(logn)
    public static int countOccurrence(String a) {
        char [] st = a.toCharArray();
        java.util.Arrays.sort(st);
        int tempsum = 0;
        for(int i=0;i<st.length;i++){
        	tempsum = searchLastOccurrence(st, st[i]) - searchFirstOccurrence(st,st[i]) + 1;
        	if (max < tempsum) 
        		max=tempsum;
        }
        return max;
    }

	public static void main(String[] args) {
        String s = "ffgggtvshjsdhjfffffffhvjbjcharu"; 
System.out.println(countOccurrence(s));
	}

}

- reda April 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class TextOcuurence {

    static int max=0;
    //Time Complexity O(logn)
    //find 
    public static int searchFirstOccurrence(char [] a, char target) {
    	
        return String.copyValueOf(a).indexOf(target);

    }

    //Time Complexity O(logn)
    public static int searchLastOccurrence(char [] a, char target) {


        return String.copyValueOf(a).lastIndexOf(target);
    }

    //Time Complexity O(logn)
    public static int countOccurrence(String a) {
        char [] st = a.toCharArray();
        java.util.Arrays.sort(st);
        int tempsum = 0;
        for(int i=0;i<st.length;i++){
        	tempsum = searchLastOccurrence(st, st[i]) - searchFirstOccurrence(st,st[i]) + 1;
        	if (max < tempsum) 
        		max=tempsum;
        }
        return max;
    }

	public static void main(String[] args) {
        String s = "ffgggtvshjsdhjfffffffhvjbjcharu"; 
System.out.println(countOccurrence(s));
	}

}

- reda April 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.testing.learn;

/**
 * Created by rajive.pai on 4/5/2017.
 */
public class LargestRepeatedString {


    public static char largestRepeatedChar(String s){

        char theChar = '*';
        char prev = '-';
        int count = 0;
        int max = 0;
        for(int i=0;i<s.length();i++){

            if(s.charAt(i) == prev){
                count ++;
            }
            else{
                if(count > max){
                    theChar = s.charAt(i-1);
                    max = count;
                    count =0;
                }
            }
            prev = s.charAt(i);
        }
        if(max > 1)
            return theChar;
        else
            return '*';

    }

    public static void main(String s[]){
        String st = "asdddddddddddddddeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeasddddddddddddsssfffffffffssdssss";
        System.out.printf("Largest Repeating character is %c", largestRepeatedChar(st));
    }
}

- Rajive Pai April 05, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var str="ff222mmmm";
var strarr=str.split("");
var i,curlong=1;curlongch=strarr[0],tlong=1;
var same=true;
for(i=1;i<strarr.length;i++)
{
	
	tch=strarr[i];
  tprev=strarr[i-1];
  if(tch==tprev){
  same=true;
  	tlong++;
  }
  else{
  	same=false;
    if(tlong>curlong)
    {
       curlong=tlong;
       curlongch=tprev;
    }
    tlong=1;
  }
}
if(same==true)
{
	if(tlong>curlong)
    {
	curlong=tlong;
  curlongch=tprev;
  }
}
alert(curlong+":"+curlongch);

- Santhosh Narayanaswamy May 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from collections import Counter

str1 = "ffgggtvshjsdhjfffffffhvjbjcharu"
dict1=dict(Counter(str1))
for key,value in dict1.items():
	if value == max(dict1.values()):
		print(key, value)

- valli June 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package test1;
import java.util.*;
import java.util.Map.Entry;


public class repeated {
	
	public static void main(String[] args) {
		
		String s="ffgggtvshjsdhjfffffffhvjbjcharu";
		char[] a=s.toCharArray();		
		HashMap <Character,Integer> hm=new HashMap();
		for (char x:a)
		{
			if(!hm.containsKey(x))
			{
				hm.put(x, 1);
			}
			else
			{
				hm.put(x,hm.get(x)+1);
			}
		}
		
		
		for (Entry<Character, Integer> e : hm.entrySet())
		{
			if(e.getValue().equals(b))
			{
				System.out.println(e.getKey()+"------>"+e.getValue());
			}
		}
					}
	
}

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

package test1;
import java.util.*;
import java.util.Map.Entry;


public class repeated {
	
	public static void main(String[] args) {
		
		String s="ffgggtvshjsdhjfffffffhvjbjcharu";
		char[] a=s.toCharArray();		
		HashMap <Character,Integer> hm=new HashMap();
		for (char x:a)
		{
			if(!hm.containsKey(x))
			{
				hm.put(x, 1);
			}
			else
			{
				hm.put(x,hm.get(x)+1);
			}
		}
		
				int b= (Collections.max(hm.values()));
		
		for (Entry<Character, Integer> e : hm.entrySet())
		{
			if(e.getValue().equals(b))
			{
				System.out.println(e.getKey()+"------>"+e.getValue());
			}
		}
					}
	
}

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

int b=(Collections.max(hm.values()));

- Anonymous July 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{{{{int b=(Collections.max(hm.values()));}}} - Madhu July 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Hashtable;
import java.util.Map;

public class MostRepetitive {

public static void main(String[] args) {

String str = "ffggggggtvshjsdhjfffffffhvjbjcharu";

printMostRepetitive(str);

}

private static void printMostRepetitive(String str) {

if (str != null && !str.isEmpty()) {

Hashtable<Character, Integer> output = new Hashtable<Character, Integer>();

int max = 1;
char previous = str.charAt(0);
output.put(previous, max);

int sum = 1;

for (int i = 1; i < str.length(); i++) {
if (str.charAt(i) == previous) {
sum++;
} else {
if (sum >= max) {
if (sum > max) {
output.clear();
}
output.put(previous, sum);
max = sum;
sum = 1;
}
}
previous = str.charAt(i);
}

for (Map.Entry<Character, Integer> entry : output.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}

} else {
System.out.println("String is empty or null!");
}
}

}

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

import java.util.Hashtable;
import java.util.Map;

public class MostRepetitive {

public static void main(String[] args) {

String str = "ffggggggtvshjsdhjfffffffhvjbjcharu";

printMostRepetitive(str);

}

private static void printMostRepetitive(String str) {

if (str != null && !str.isEmpty()) {

Hashtable<Character, Integer> output = new Hashtable<Character, Integer>();

int max = 1;
char previous = str.charAt(0);
output.put(previous, max);

int sum = 1;

for (int i = 1; i < str.length(); i++) {
if (str.charAt(i) == previous) {
sum++;
} else {
if (sum >= max) {
if (sum > max) {
output.clear();
}
output.put(previous, sum);
max = sum;
sum = 1;
}
}
previous = str.charAt(i);
}

for (Map.Entry<Character, Integer> entry : output.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}

} else {
System.out.println("String is empty or null!");
}
}

}

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

import java.util.Hashtable;
import java.util.Map;

public class MostRepetitive {

    public static void main(String[] args) {

        String str = "ffggggggtvshjsdhjfffffffhvjbjcharu";

        printMostRepetitive(str);

    }

    private static void printMostRepetitive(String str) {

        if (str != null && !str.isEmpty()) {

            Hashtable<Character, Integer> output = new Hashtable<Character, Integer>();

            int max = 1;
            char previous = str.charAt(0);
            output.put(previous, max);

            int sum = 1;

            for (int i = 1; i < str.length(); i++) {
                if (str.charAt(i) == previous) {
                    sum++;
                } else {
                    if (sum >= max) {
                        if (sum > max) {
                            output.clear();
                        }
                        output.put(previous, sum);
                        max = sum;
                        sum = 1;
                    }
                }
                previous = str.charAt(i);
            }

            for (Map.Entry<Character, Integer> entry : output.entrySet()) {
                System.out.println(entry.getKey() + " -> " + entry.getValue());
            }

        } else {
            System.out.println("String is empty or null!");
        }
    }

}

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

private static char getMaximumCharacter(String s) {
if (s == null || s.length() == 0) {
return '\0';
}

int maxCount = Integer.MIN_VALUE;
char res = '\0';
int i = 0;
int j = 0;
while (j < s.length()) {
if (s.charAt(i) == s.charAt(j)) {
j++;
} else {
int tmp = j - i;
if (tmp > maxCount) {
maxCount = tmp;
res = s.charAt(i);
}
i = j;
}
}

if (j - i > maxCount) {
res = s.charAt(i);
}


return res;
}

- matt September 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static char getMaximumCharacter(String s) {
if (s == null || s.length() == 0) {
return '\0';
}

int maxCount = Integer.MIN_VALUE;
char res = '\0';
int i = 0;
int j = 0;
while (j < s.length()) {
if (s.charAt(i) == s.charAt(j)) {
j++;
} else {
int tmp = j - i;
if (tmp > maxCount) {
maxCount = tmp;
res = s.charAt(i);
}
i = j;
}
}

if (j - i > maxCount) {
res = s.charAt(i);
}
System.out.println(maxCount + " : " + res);

return res;
}

- Matt September 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{ { public static void main(String[] args) {
String str = new String("ffgggtvshjsdhjfffffffhvjbjcharu");
HashMap<Character, Integer> map = new HashMap<Character, Integer>();

int counter = 1;
int flag = 0;
int max = 0;
int storeIndex = 0;


for (int i = 0; i < str.length() - 1; i++) {
if (str.charAt(i) == str.charAt(i + 1)) {
counter = counter + 1;
flag = 1;
}

else if (max < counter && flag == 1) {
max = counter;
storeIndex = i;
flag = 0;
counter = 1;
}
}
}}}

- ajnavi July 31, 2018 | 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