Amazon Interview Question for SDE1s


Country: United States
Interview Type: Phone Interview




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

you can use hash_table to do the group job.
use the list2<sex, occupation> as the key, and the value will the index in the list1.
so after iterator the list1, you will get the group by result.

- suwei19870312 December 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class People {
    String name;
    String position;
    String sex;
    People(String name, String position, String sex){
        this.name = name;
        this.position = position;
        this.sex = sex;
    }
    public String getAttribute(String attrName){
        if(attrName == null)
            return null;
        if(attrName.equalsIgnoreCase("name"))
            return name;
        else if(attrName.equalsIgnoreCase("position"))
            return position;
        else if(attrName.equalsIgnoreCase("sex"))
            return sex;
        else
            return null;
    }

    private static Map<List<String>, List<People>> groupAllByAttributes(List<People> friends, List<String> attributes){

        Map<List<String>, List<People>> result = new HashMap<List<String>, List<People>>();
        for(People p : friends){
            List<String> atrb = new ArrayList<String>();
            List<People> people;

            for (String a : attributes){
                atrb.add(p.getAttribute(a));
            }
            if(result.get(atrb) == null) {
                people = new ArrayList<People>();
                people.add(p);
                result.put(atrb,people);
            }
            else{
                people = result.get(atrb);
                people.add(p);
                result.put(atrb,people);
            }
        }
        return result;
    }

    @Override
    public String toString() {
        return name;
    }

    public static void main(String [] args){
        List<People> friends = new ArrayList<People>();
        List<String> attributes = new ArrayList<String>();
        attributes.add("sex");
        attributes.add("position");

        friends.add(new People("A", "Fin", "male"));
        friends.add(new People("B", "Eng", "female"));
        friends.add(new People("C", "Doc", "male"));
        friends.add(new People("D", "Eng", "female"));
        friends.add(new People("E", "Doc", "male"));
        friends.add(new People("F", "Fin", "male"));
        friends.add(new People("G", "Pharma", "male"));
        friends.add(new People("H", "Pharma", "male"));
        friends.add(new People("I", "Fin", "female"));

        System.out.println(groupAllByAttributes(friends, attributes));

    }
}

- Milan December 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void GroupByMethod(std::list<people>& friends, std::list<string>& attributes) {
    std::map<list<string>,list<people> > mapbyGroup;
    std::map<list<string>,list<people> >::iterator it_map;
    list<people>::iterator it;
    list<string>::iterator it_str;
    for (it = friends.begin(); it != friends.end(); it++) {
        list<string> attribList;
        
        for (it_str = attributes.begin(); it_str != attributes.end(); it_str++) {
            attribList.push_back(it->getAttribute(*it_str));
        }
        it_map = mapbyGroup.find(attribList);
        if (it_map != mapbyGroup.end()) {
            it_map->second.push_back(*it);
        } else {
            list<people> peopleList;
            peopleList.push_back(*it);
            mapbyGroup.insert(std::pair<list<string>,list<people> >(attribList, peopleList));
        }
    }
    friends.clear();
    for (it_map = mapbyGroup.begin(); it_map != mapbyGroup.end(); it_map++) {
        friends.insert(friends.end(), it_map->second.begin(), it_map->second.end());
    }
}

- addy December 05, 2014 | Flag Reply


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