Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
Howz this ?
#include <iostream>
using namespace std;
int main (int argc, const char * argv[])
{
int n;
cin >> n;
int num, count = 0, prev_num, max_num, max_count = 0;
for(int i = 0;i < n; i++)
{
cin >> num;
if(i == 0)
{
count++;
}
else
{
if(num == prev_num)
{
count++;
}
else
{
count = 1;
}
}
prev_num = num;
if(count > max_count)
{
max_num = num;
max_count = count;
}
}
cout << max_num << max_count;
return 0;
}
void maxCount(int a[], int len) {
int i = 0;
int maxNum, maxCount = 0, curr_num, curr_count = 0;
for(i = 0; i < len; i++) {
if(a[i] == curr_num) curr_count++;
else {
if(curr_count > max_count) {
max_count = curr_count;
max_num = curr_num;
}
curr_count = 1;
curr_num = a[i];
}
}
printf("%d has maximum count of %d", max_num, max_count);
}
public class MaxContinousRepeatChar {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(getMaxContinuosRepeatedChar("1122333344556677777".toCharArray()));
}
public static char getMaxContinuosRepeatedChar(char[] ch){
if(ch == null || ch.length==0)
return '\0';
if(ch.length==1)
return ch[0];
char maxCh = ch[0];
int countMaxCh = 1;
char prevCh = ch[0];
int countPrevCh = 1;
for(int i=1; i < ch.length ; i++){
if(prevCh != ch[i]){
if(countMaxCh < countPrevCh){
maxCh = prevCh;
countMaxCh = countPrevCh;
}
prevCh = ch[i];
countPrevCh = 1;
}else{
countPrevCh++;
}
}
//for the last char
if(countMaxCh < countPrevCh){
maxCh = prevCh;
countMaxCh = countPrevCh;
}
return maxCh;
}
}
public static int getMaxOccurrance(int array[])
{
int currCount = 0, maxCount = 0, currElement = 0, maxElement = 0;
currElement = maxElement = array[0];
for(int i = 0; i < array.length; i++)
{
if(array[i] == currElement)
{
currCount++;
if(maxCount < currCount)
{
maxCount = currCount;
maxElement = array[i];
}
}
else
{
currCount = 1;
currElement = array[i];
}
}
return maxElement;
}
#include<stdio.h>
int main()
{
int n,a[100],i,count=1,count1=1,item;
printf("Enter the no of item\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]==a[i+1])
{count++;}
else
{
if(count>=count1)
{
item=a[i];
count1=count;
}count=1;
}
}
printf("The most repeated(consecutive) item is %d and repeated %d times\n",item,count1);
return 0;
}
int maxNoTimes(int arr[], int len)
{
int count=0;
int count2=1;
int max_count=0;
if(len < 1)
return 0;
if(len < 2)
return 1;
while(count2<len)
{
while(arr[count2]==arr[count] && count2 < len)
{
count2++;
}
if((count2-count) > max_count)
max_count = count2-count;
if(count2<len)
{
count = count2;
count2++;
}
}
return max_count;
}
- Anonymous January 27, 2012