Coding Challenge

I’m currently in the very early stages of the application process for a Software Engineering position. After completing the initial online application, they e-mailed me a link for a “90 minute coding challenge” for me to complete whenever I had the time. This post will cover the problems I was presented and my solutions to them. The problem description will be in my own words since there is no way for me to go back and check the originals, but hopefully all the constraints will be properly conveyed.


Problem 1: Duplicate E-mail Address Detection

Take this e-mail for example: “username@example.com”. This e-mail address (and all other e-mail addresses) is made up of two parts:  The local name and the domain name. In this scenario, the local name is “username” and the domain name is “example.com”. These two parts are separated by the @ sign.

The task at hand is to spot “duplicate” e-mail addresses. If there is a “.” in the local name, we want to forward all e-mails to it’s equivalent e-mail address without the “.”‘s in it. For example: “user.name@example.com” and “user…..name@example.com” become “username@example.com”.

Additionally, if there is a “+” sign in the local name, we ignore all the characters after it. For example: “abc123+456@example.com” and “abc.123…+456@example.com” are the same as “abc123@example.com”.

Additionally, if there are additional “.”‘s in the domain name, we must treat that as a different email. This means that “username@example.com” is different from “username@exa.mple.com”.

Test case:

{"a.b@example.com", "x@example.com", "x@exa.mple.com", "ab+1@example.com", "y@example.com", "y@example.com", "y@example.com"};

This should return “2” because there are two pairs of duplicate e-mail addresses (y@example.com vs y@example.com and a.b@example.com vs ab+1@example.com).

My Solution:

import java.util.ArrayList;
public class Solution1 {
    // whenever we find a unique e-mail address we will store it in this
    ArrayList<String> emails = new ArrayList<>();

    public int solution(String[] L) {
        int numDuplicates = 0;
        for (String curr : L) {
            // split each e-mail address so that it's local name is separate from it's domain name
            String[] emailAddress = curr.split("@");

            // isolate the local name
            String localName = emailAddress[0];
            String result = "";

            // iterate through the whole local name
            for (int i = 0; i < localName.length(); i++) {
                // if we find a . we continue without adding it. If we find a + we stop iterating
                if (localName.charAt(i) == '.') {
                    continue;
                } else if (localName.charAt(i) == '+'){
                    break;
                } else { // add substring to result
                    result += localName.substring(i);
                }
            }

            // make a properly formatted email address
            String formatAdd = result + "@" + emailAddress[1];
            
            // check if that email address is already in our list of unique addresses
            if (emails.contains(formatAdd)) {
                // if it is, we increment our duplicates variable
                numDuplicates++; 
            } else {
                emails.add(formatAdd);
            }
        }
        return numDuplicates;
    }
}

Problem 2: Amy the Fruit Picker

Amy the fruit picker carries two baskets while harvesting her fruit and likes to only have one type of fruit in each basket. Our goal here is to find the maximum amount of fruit that Amy can harvest at one time.

  • Amy walks into the orchard and up to a tree with her initially empty baskets. She puts the fruit from the first tree into her first empty basket and then moves on to the next tree.
    • If the fruit in the second tree is the same as the fruit in the first tree, she puts that fruit in the first basket as well. This process continues until a second type of fruit is found.
    • If the fruit is different from the fruit in the first basket, she puts the new fruit in the second basket and moves onto the next tree.
      • When Amy get’s to the third tree, if the fruit is the same as the fruit in the first or second basket, she adds it to it’s respective basket.
      • If the fruit is of a different type, Amy stops collecting and heads back.

We are given an array that represents types of fruit in an orchard. For example, we receive an array:  {1,2,1,2,1,4,3,4,3,3,3,4,3,4,3,5}. The output from this array should be 10 because of the highlighted sequence: {1,2,1,2,1,4,3,4,3,3,3,4,3,4,3,5}. Our range of possible array elements are integers [1..9].

My Solution:

public class Solution {
    public int solution(int[] A) {
        // num1 and num2 represent the types of fruit in each basket
        int num1 = 0, num2 = -1;
        int collected = 0;
        int maxCollected = 0;

        // iterate through all the numbers in the array
        for (int curr : A) {
            if (num1 == 0) { // if we have not assigned a fruit type for the first basket
                collected++;
                num1 = curr;
                continue;
            } else if (num2 == -1 && curr != num1) { // check to make sure the second tree isn't the same as num1
                collected++;
                num2 = curr;
                continue;
            }

            // if the fruit is one of the two kinds in Amy's basket, we collect it
            if (curr == num1 || curr == num2) {
                collected++;
            } else { // if not, we "dump" all the fruit and move on to the next two kinds of fruit
                // if we collected more than the last sequence, assign that value to max collected
                if (collected > maxCollected)
                    maxCollected = collected;

                // the new type of fruit in basket 1 becomes our current fruit type
                collected = 1;
                num1 = curr;
                num2 = -1;
            }


        }

        if (maxCollected == 0) // in case we only have 1 working sequence of fruit 
            maxCollected = collected;
        return maxCollected;
    }
}

All in all I was very pleased with myself and the fact that I was able to complete this challenge in ~40 minutes even though they allowed for me to spend 90 minutes on it.

Leave a Reply

Your email address will not be published. Required fields are marked *