Google Interview Question for SDE1s


Country: United States




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

Hierarchical data structure:

package com.cracking.google;

public class EnumParentChild {
	
	static enum Vehicle {
		car(null,1),
			toyota(car,11),
				camry(toyota,111),
				coroloa(toyota,112),
			honda(car,12),
		track(null,2),
			GMC(track,21);
		
		Vehicle(Vehicle parent,int value) {
			this.parent = parent;
			this.value = value;
		}
		
		public Vehicle parent;
		public int value;
	}
	

	public static void main(String[] args) {
		System.out.println(Vehicle.GMC + " " + Vehicle.GMC.value);
		System.out.println(Vehicle.GMC.parent + " " + Vehicle.GMC.parent.value);
	}

}

Output:
GMC 21
track 2

- ProTechMulti November 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

not sure how this design will scale, but anyway, I understood we have:
- enum integer values that are hirarchical, e.g. each decimal place represents a hirarchy
- there is a way to lookup the textual representation of the enum value e.g. there is a hashtable with integer key and text representation value
- you want the parent-key of a key? that is integer division by 10
- I wonder what you do if there are more then 10 children .. but anyway, here the code

unordered_map<int, string> enums_{{1, "car"}, {11, "toyota"}, {111, "prius"} /*, ... */ }; 

	pair<int, string> get_parent(int value) {
		if(value >= 10) {
			auto it = enumns_.find(value/10);
			if(it != enums.end()) {
				return it->second;
			}
		}
		throw "error, has no parent (?)";
	}

- Chris November 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its a composite design pattern, we are supposed to reuse the parent value.

/**
 * give an enum, where each element has a parent-children relationship, and children's expression is the value of parent append an index, such as
 * enum vehicle {
 *          car = 1
 *              toyota = 11
 *                   camry = 111
 *                   corolla = 112
 *              honda = 12
 *         truck = 2
 *              GMC = 21
 * }
 *
 */
public class ParentChild {

    public enum Vehicle {
        CAR("1",null),
            TOYATA("1",CAR),
                CAMERY("1",TOYATA),
                CORALLA("2",TOYATA),
            HONDA("2",CAR),
        TRUCK("2",null),
            GMC("1",TRUCK)
        ;

        String value;
        Vehicle(String val, Vehicle parent) {
            if(parent!=null) {
                this.value = parent.value+val;
            } else {
                this.value = val;
            }

        }

        @Override
        public String toString() {
            return name()+":"+value;
        }
    }

    public static void main(String[] args) {
        System.out.println(Vehicle.TOYATA);
        System.out.println(Vehicle.CORALLA);
        System.out.println(Vehicle.GMC);
    }
}

- Ganesh July 15, 2018 | 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