Apple Interview Question for Backend Developers


Country: United States
Interview Type: Phone Interview




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

Looking for interview experience sharing and mentor?

Visit aonecode.com for private lessons by FB, Google and Uber engineers

Our ONE TO ONE class offers

SYSTEM DESIGN Courses (highly recommended for candidates for FLAG & U)
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algos & Clean Code),
latest interview questions sorted by companies,
mock interviews.

Our students got hired from G, U, FB, Amazon, LinkedIn and other top tier companies after weeks of training.

Feel free to email us aonecoding@gmail.com with any questions. Thanks!

Solution
For the first problem a hash set of {IPs} may simply solve it.

However in the follow-ups as the amount of data cumulates, if the IPs are stored as strings, 4GB is becomes absolutely insufficient, since there are over 4 billion IPs out there and each ip as a string takes at least 9 bytes.
Even if convert ip to integer(8B), it wouldn't be enough.

An approach is to go with bit set. Then each ip takes only 1 bit. Have a bit set to store whether an ip occurs and another set to store whether an ip repeats.

This takes 2 * 2^32 bit = ~1GB -> fits in RAM.

public class IPFilter {

    long[] map;          //mark all ip that showed up
    long[] repeatedIP;    //mark all ips that repeatedly showed up

    public IPFilter() {
        //there's 2^32 IP in total.
        // each long integer is identifies 64 IPs.
        // Need 2^32 / 2^6 long integers in the bit map
        int size = 1 << (32 - 6);
        map = new long[size];
        repeatedIP = new long[size];
    }

    public void addToMap(List<String> IPs) {
        for(String ip: IPs) {
            long decimal = IPToLong(ip);
            int idx = (int)(decimal / 64);
            int res = (int)(decimal % 64);

            if((map[idx] >> res) == 1) {    //repeated ip
                repeatedIP[idx] |= (1 << res);
            } else {                        //first occurred ip
                map[idx] |= (1 << res);
            }
        }
    }

    private long IPToLong(String ipAddress) { //convert ip (base 256) to decimal
        String[] ipAddressInArray = ipAddress.split("\\.");
        long result = 0;
        for (int i = 0; i < ipAddressInArray.length; i++) {
            int power = 3 - i;
            int ip = Integer.parseInt(ipAddressInArray[i]);
            result += ip * Math.pow(256, power);
        }
        return result;
    }
 }

- aonecoding July 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@aonecoding, I believe

if((map[idx] >> res) == 1

should be

if((map[idx] >> res) % 10 == 1

. This will get the bit value at location res from the bitset

- Arunava Saha November 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

It is better to avoid writing a single line of code, when you can write it in 2 lines.
[ stackoverflow.com/questions/618378/select-unique-or-distinct-values-from-a-list-in-unix-shell-script ]

./yourscript_to_isolate_ip.sh | sort | uniq -u

and this, will run no matter how less memory you have.
Aws guys actually run it, so does azure folks. I am pretty sure Apple will do the same.

- NoOne July 23, 2017 | 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