Amazon Interview Question for SDE-2s

Country: United States
Interview Type: In-Person

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

package com.home.careercup;

To support age range query - parse all records and maintain age counts
in array where a [i] = c if there are c people of age i in input.
For a query with age range [q,r], we can now sum up values 
 a[q], a[q+1] .. a[r-1], a[r].
It is a good idea to curate the input age range before applying to the
public class AgeRangeQuery {

    static int MAX_AGE=99;
    static int[] ageData = new int[MAX_AGE +1];

    public static void main(String[] args) {
        Person[] persons = megaParse();

        for (Person p : persons) {
            if (p.age < 100) ageData[p.age]++;
        int ranges[][] = new int[][]{
                {11, 44},
                {33, 55},
                {71, 81},
                {91, 100},
                {98, 100}
        for ( int [] range: ranges){
            System.out.println(rangeCount(range [0], range [1]));

    static  int rangeCount(int age1, int age2) {
        int low, high;
        if (age1 <= age2) {
            low = age1;
            high = age2;
        } else {
            low = age2;
            high = age1;
        // finally adjust ranges
        low = ( low <0) ? 0: low;
        high = (high> MAX_AGE) ? MAX_AGE: high;
        int result = 0;
        for (int i = low; i <= high; i++) result += ageData[i];
        return result;


    private static Person[] megaParse() {
        return new Person[]{
                new Person(1), new Person(2), new Person(9),
                new Person(11), new Person(12),
                new Person(33), new Person(44),
                new Person(55),
                new Person(60),
                new Person(71),
                new Person(81),
                new Person(91) //etc

    static class Person {
        public Person(int age) {
            this.age = age;

        String name;
        int age;
        // etc

- Makarand August 30, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
of 1 vote

Store the number the people in an array of long ages[]=new long[150], assuming 150 of max age.For each incoming age, we can update corresponding index and increment by 1.

We can actually leverage segment tree here for better performance.

- koustav.adorable August 30, 2017 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


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


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