Amazon Interview Question
Testing / Quality AssurancesCountry: India
Interview Type: Written Test
// 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 )
- (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);
}
}
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();
}
}
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;
}
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;
}
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();
}
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);
}
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);
}
#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")
#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 */
#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 */
// 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);
}
// 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);
}
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);
}
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);
}
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);
}
}
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);
}
}
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));
}
}
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));
}
}
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));
}
}
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));
}
}
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);
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());
}
}
}
}
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());
}
}
}
}
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!");
}
}
}
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!");
}
}
}
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!");
}
}
}
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;
}
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;
}
{{ { 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;
}
}
}}}
Easy as below in C#
}
- maksymas March 14, 2017