Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

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

public class Max_Continuous_Element {

	public static void main(String[] args) {
		int[] a = { 2, 1, 1, 1, 1, 5, 5, 6, 6, 6, 6, 6, 6, 6, 1, 1, 1, 1, 2, 2,
				2, 2, 6, 2, 2, 6, 2 };

		try {
			System.out.println(find_max(a));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static int find_max(int[] a) throws Exception {

		int max_var = 0, max_count = 0, counter = 1;

		max_var = a[0];

		for (int i = 1; i < a.length; i++) {
			if (a[i] == a[i - 1])
				counter++;

			if (counter >= max_count) {
				max_var = a[i - 1];
				max_count = counter;
			}

			if (a[i] != a[i - 1]) 
				counter = 1;
		}

		return max_var;
	}
}

- Jester January 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I suppose, above code will work if the array is sorted. Please correct me if I am wrong.

- CyberCoder January 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#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;
}

- Mickey January 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}}

- unemployed coder January 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Shreyas January 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;

- unemployed coder January 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Betters solution would be to
1. Sort using quick sort -O(nlogn)
2Then keep track of max values

- Niharika January 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Betters solution would be to
1. Sort using quick sort -O(nlogn)
2Then keep track of max values

- Niharika January 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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");

- Satya Vijay January 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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 ");		
	}
}

- Gowtham January 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#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);
}

- Raghu February 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
	
}
}

- Thahir February 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is a very trivial problem. i guess the easiest question asked by Amazon
(nobrainer .co .cc)

- Anony February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#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;
}

- popoff February 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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);
}

}

- Manoj Parida February 18, 2012 | Flag


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