Skip to content

Instantly share code, notes, and snippets.

@theopendle
Created January 23, 2022 10:27
Show Gist options
  • Save theopendle/c38a554cd9af83a31787bfb42ea77eca to your computer and use it in GitHub Desktop.
Save theopendle/c38a554cd9af83a31787bfb42ea77eca to your computer and use it in GitHub Desktop.

Q1. What is the output of this program?

public class Test {
    public static void main(String[] args) {
        int n = 10;
        int next = 0;
        int nextNext = 1;
        for (int i = 1; i < n; i++) {
            nextNext = next + nextNext;
            next = nextNextnext;
        }
        System.out.println(nextNext);
    }
}

Answer

For the sake of clarity, I renamed next to a and nextNext to b:

public class Test {
    public static void main(String[] args) {
        int n = 10;
        int a = 0;
        int b = 1;
        for (int i = 1; i < n; i++) {
            b = a + b;
            a = ba;
        }
        System.out.println(b);
    }
}

Then simplified the logic:

  • a is added to b
  • a is set to a removed from b

Therefore:

  • a takes on the previous value of b

Therefore:

  • the previous value of b is added to b

Therefore:

  1. 1 0 1
  2. 1 1 2
  3. 2 1 3
  4. 3 2 5
  5. 5 3 8
  6. 8 5 13
  7. 13 8 21
  8. 21 13 34
  9. 34 21 55

The result is 55.

2. Write a method in a programming language of your choice that calculates x^y

a. How does the method look for x,y where x,y are integers (e.g. 2^3 = 8)?

b. How does the method look for x,y where x,y are non-integers (e.g. 2^3.1415 = 8.824411)?

Answer

JS and Python already have exponent operators. In JS I'm sure, and in Python I think, that the exponent operator supports non-integers.

For the sake of this exercise then, I will use Java and primitive types.

My first, naive approach in O(n)

public static long powerOfN(int x, int exponent) {
    if (exponent == 0) return 1;

    long result = x;
    for (int i = 0; i < exponent - 1; i++) {
        result = result * x;
    }
    return result;
}

My second approach in O(logn), given that 2^4 = 2^2 * 2^2

public static long powerOfLogN(int x, int exponent) {
    if (exponent == 0) return 1;
    if (exponent == 1) return x;

    if(exponent % 2 == 0) {
        return powerOfLogN(x, exponent/2) * powerOfLogN(x,exponent/2);
    } else {
        return x * powerOfLogN(x, exponent - 1);
    }
}

My solution if x is a non-int:

public static double powerOfLogNDoubleExponentInt(double x, int exponent) {
    if (exponent == 0) return 1;
    if (exponent == 1) return x;

    if(exponent % 2 == 0) {
        return powerOfLogNDoubleExponentInt(x, exponent/2) * powerOfLogNDoubleExponentInt(x,exponent/2);
    } else {
        return x * powerOfLogNDoubleExponentInt(x, exponent - 1);
    }
}

Unfortunately I was not able to find a solution within a reasonable timeframe for the case where the exponent is a non-int. I'd have to go back to my math lessons from highschool to remember how decimal exponents work. Same goes for negative exponents, although they are not mentioned in the question.

Possible improvements:

  • The precision of doubles is not exact, Java has a BigDecimal class that can be used to this effect.

3. What is a deadlock?

Answer

A deadlock is a situation that can be expressed with the following example:

  • Processes P1 and P2 both require resources R1 and R2
  • P1 locks R1
  • P2 locks R2
  • P1 attempts to use R2
  • P2 attempts to use R1
  • Both processes are blocked as neither can use both resources simultaneously

4. Jointly working in a project, what are the advantages, what are the challenges?

I'm wasn't sure if this meant 'Multiple people working on a project in one team' or 'Multiple teams working together on a project', so I answered for both.

Multiple people working on a project in one team vs working alone

Pros:

  • Code and decisional quality: More eyes and ears = fewer misunderstandings and mistakes
  • Speed: Being able to parallelise parts of a project is an advantage
  • Morale: Knowing you are not alone in tough times is good for the psyche
  • Knowledge sharing: Pairing more and less experienced workers or workers with different specialities allows for each individual to learn and improve the average competency level of the team

Cons:

  • Organisational overhead: More people = more oragnisation which can eat into development time, especially at the beginning of the teams' existence

Multiple teams working together on a project vs a single team

Pros:

  • Specialisation: Each team can provide it's particular expertise to a project. Having more people means each person has a reduced scope of responsibilities and can therefore aquire deeper knowloedge rather than broader knowledge.

Cons:

  • Priorities: Typically a team is headed by a manager. Two teams = two leaders on the same hierarchical level, which can cause mis-alignments with regards to priorities in the project.
  • Administrative overhead: Different teams may have different tooling, different backlogs, have different intra-team structure, etc. This may generate administrative overhead for the team members and managers.

5. Databases and SQL

1. All customers from Switzerland (COUNTRIES.MNEMONIC = ‘CH’)

select cu.* from customers as cu
inner join addresses as a on a.customer_id = cu.id
inner join countries as co on a.country_id = co.id
where co.mnemonic = 'CH'

2. All customers without addresses

select cu.* from customers as cu
left join addresses as a on a.customer_id = cu.id
where a.id is null 

3. All customers with CITY + COUNTRY. For customers without addresses CITY and COUNTRY remain empty.

Assumptions:

  • 'CITY + COUNTRY' refers to a single column result containing both the city and the country
select cu.*, CONCAT(co.name, ", ", a.city) from customers as cu
left join addresses as a on a.customer_id = cu.id
left join countries as co on a.country_id = co.id

Disclaimer: I'm not certain that this query will work if the co.name or a.city are null. Nor am I certain for which SQL dialects CONCAT() exists (I think Oracle at least) and whether it accepts a list or must be chained to include the comma separator. I would have to look those details up.

6. Data model for a quiz

1. Which data model would you propose?

Others concerns:

  • Answers must have a unique key pair for employees and questions
  • The value of an answer might be mappable to and from a VARCHAR or need to be typed (eg: boolean_value, string_value, number_value, etc.)

2. Does a three-tier architecture make sense? Advantages, disadvantages?

Assumptions:

  • 'three-tier architecture' no longer refers to a data model, but a Client-Server-Database architecture.
  • I am being asked to compare 3-tier vs 2-tier architecture

3-tier architecture seems superior to 2-tier in almost every way. The only use-case I could imagine for a 2-tier architecure would be if this quiz was an informal system that existed within a team of people who are all comfortable interacting with an SQL database. In this case, the client might by SQL Developer or sqlplus or equivalent and the only development required would be the data model.

Some advantages of a 3-tier architecture:

  • Security: No direct access to database. All possible queries exist on the Application layer, no freestyle querying is possible. Subnetwork filtering can assure that only employees within local network can access the database. Etc.
  • Authentication: I don't know which databases support LDAP/AD integration, but this could be done easily with an application layer, removing the need to issue/maintain personal database accounts
  • Data quality: Enhanced data validation is possible to avoid unwanted data
  • Better UX: No installation of heavy client is required, nor is knowledge of CLI. Client layer can be served as a webpage.
  • Modularity and maintenance: Business logic, data model and supporting stack can be updated without needing to interact with each employee's workstation.
  • Traceability: Logging at the Application layer provides more granularity that at the database layer and is a central point of traceability as opposed to the client layer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment