Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
#include <iostream>
using namespace std;
int element(int arr[], int len)
{
int counter, maxCounter, temp, j;
counter = maxCounter = 1;
for(int i=0,j = 1; j < len; i++,j++)
{
if(arr[i] == arr[j])
{
counter++;
if(maxCounter < counter)
{
maxCounter = counter;
temp = arr[i];
}
}
else
counter = 1;
}
return temp;
}
int main()
{
int arr[] = {1,1,1,3,3,3,3,2,4,4,1,1,1};
int max = element(arr,13);
cout<<"Max occured element is " <<max<<endl;
getchar();
return 0;
}
Just use hash-table (or similar). If the array just contains digits 0-9, you could also further simplify, with just an array of size 10.
{{
int findMax (int[] nums)
{
int[] table = new int[10];
//initialize each index with value 0;
// then count each item
for (int i = 0; i < nums.length; i++){
table[ nums[ i ] ]++;
}
//now find the max
int max = table[0];
for(int i =0 ; i < table.length; i++)
{
if ( table[i] > max){
max = table [i];
}
return max;
}}
Hi there,
I am sorry, but I think your code will fail for 1,2,2,2,2,1,2,1,3,1,4,1,1. You code will return 6 bcuz number of ones is 6 but the actual answer expected is 4 that is number 2 which repeats 4 consecutive number of times.
What your code does is, it will find the number which appears maximum number of times in an array not the maximum consecutive number of times.
oops, I need 3
. Better formatted.
int findMax (int[] nums)
{
int[] table = new int[10];
//initialize each index with value 0;
// then count each item
for (int i = 0; i < nums.length; i++){
table[ nums[ i ] ]++;
}
//now find the max
int max = table[0];
for(int i =0 ; i < table.length; i++)
{
if ( table[i] > max){
max = table [i];
}
return max;
System.out.println("Enter number: ");
DataInputStream dis=new DataInputStream(System.in);
String s=dis.readLine();
int count=1,i;
int count2=0;
int num=Integer.parseInt(s.charAt(0)+"");
char c=s.charAt(0);
for( i=1;i<s.length();i++)
{
if(c==s.charAt(i))
count++;
else
{
c=s.charAt(i);
if(count>count2)
{
count2=count;
num=Integer.parseInt(s.charAt(i-1)+"");
}
count=1;
}
}
if(count>count2)
System.out.println("\n The most common number is "+s.charAt(i-1)+" and it is repeated "+count+" times");
else
System.out.println("\n The most common number is "+num+" and it is repeated "+count2+" times");
public class Occurence {
public static void main(String[] args) {
char[] a = "1113333244111".toCharArray();
char temp, maxval = ' ';
int max = 0, k = 1;
for(int i=1; i < a.length; i++)
{
if(a[i-1] == a[i])
{
k++;
if(k > max)
{
max = k;
maxval = a[i];
}
}
else
k=1;
}
System.out.println(max+ " "+maxval+"s occured consecutively ");
}
}
#include <stdio.h>
int arr[] = {1,2,2,2,2,1,2,1,3,1,4,1,1};
main()
{
int max_sofar=1,max_elem=arr[0];
int cur_max = 1,cur_elem = arr[0];
int i;
for (i=1;i<13;i++) {
if (arr[i] == arr[i-1]) {
cur_max++;
} else {
cur_max = 1;
cur_elem=arr[i];
}
if (cur_max > max_sofar) {
max_sofar = cur_max;
max_elem = cur_elem;
}
}
printf("elem=%d times=%d\n",max_elem,max_sofar);
}
public class fiding_max_consective_numbers {
public static void main (String args[])
{
int numbers [] = {1,1,2,3,3,4,4,4,4,4,4,4,4,4,4,2,1,3,3,3,3,3};
int count =1;
int max =1;
for(int i = 0;i<numbers.length-1;i++)
{
if(numbers[i]==numbers[i+1])
{
count++;
if(count>max)
{
max = count;
}
}
else
{
count =1;
}
}
System.out.println(max);
}
}
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int arr[] = {1,1,1,3,3,3,3,2,4,4,1,1,1};
int size = sizeof(arr)/sizeof(arr[0]);
int cur_len=1,cur_max=1,max_len=1,index =0;
for (int i=0 ; i<size ; i++)
{
if (arr[i]==arr[i+1])
{
cur_len++;
}
else
{
cur_max = cur_len ;
cur_len = 1;
}
if (cur_max>max_len)
{
max_len = cur_max ;
index = i-max_len+1;
}
}
cout << endl << max_len;
cout << endl << index;
}
package com.amazon;
/*
* Write code for the following problem:
find the element in the array that appears consecutively in the array max number of times. Should be O(N)
eg. 1113333244111 ans. 3
*/
public class MaxOccNumInArr {
static String num = "1113333244111";
public static void main(String[] args) {
int i = 0;
int start = 0;
int end = 0;
String charSeq="";
String prevCharSeq="";
for(i = 1 ; i < num.length() ; i++){
if(num.charAt(i)==num.charAt(i-1)){
end++;
charSeq=num.substring(start, end+1);
if(charSeq.length()>prevCharSeq.length()){
prevCharSeq=charSeq;
}
System.out.println(charSeq);
}else{
start=i;
end=i;
charSeq="";
}
}
System.out.println("Longest Char Seq::"+prevCharSeq);
}
}
Keep 3 local variables to keep an account of the max variable so far, max count so far, and a normal counter variable.
Algo:
1. if the value at current index == value at last index, increment the counter.
2. if the value of the counter is > max_counter, update max_counter and max_var.
3. if the value at current index != value at last index, restart counter
- Jester January 27, 2012