@Override
    public String findCommonAncestor(String[] commitHashes,
                                     String[][] parentHashes,
                                     String commitHash1,
                                     String commitHash2) {
        String max;
        String min;
        int diff = commitHash1.compareTo(commitHash2);
        if (diff > 0) {
            max = commitHash1;
            min = commitHash2;
        } else if (diff < 0) {
            max = commitHash2;
            min = commitHash1;
        } else {
            return commitHash1;
        }

        int index = asList(commitHashes).indexOf(max);
        Set<String> parentsOfMax = new HashSet<>(asList(parentHashes[index]));
        Set<String> parentsOfMin = new HashSet<>(asList(new String[]{min}));
        for (++index; index < commitHashes.length; ++index) {
            String commitHash = commitHashes[index];

            if (parentsOfMax.remove(commitHash)) {
                if (parentsOfMin.contains(commitHash)) {
                    return commitHash;
                }

                parentsOfMax.addAll(asList(parentHashes[index]));
            } else if (parentsOfMin.remove(commitHash)) {
                parentsOfMin.addAll(asList(parentHashes[index]));
            }
        }

        return null;
    }