Skip to content

Instantly share code, notes, and snippets.

@influentcoder
Last active May 18, 2019 09:22
Show Gist options
  • Save influentcoder/3817ce597eebc04036aa2ff009f567f6 to your computer and use it in GitHub Desktop.
Save influentcoder/3817ce597eebc04036aa2ff009f567f6 to your computer and use it in GitHub Desktop.
Interview Questions

Apples and Orange problem

In front of you are 3 boxes. One box contains only apples, another box contains only oranges, and the last contains both apples and oranges. The first box has the label "apples," the second "oranges," and the third "apples and oranges." Unfortunately all of the labels are wrong. Your job is to fix the labels. You are not allowed to peek inside any of the boxes. But you can ask for a sample from any box. You point to a box, and you get a fruit from that box. What is the minimum number of samples you need to label all of the boxes correctly?

http://mkcohen.com/saturday-puzzle-9-apples-and-oranges

Number of squares for an NxN chessboard?

What if we want to find the number of squares for a chessboard of size NxN, where N is any number. Do you notice a pattern in the results in the table above? Well, if you take a look at the results that we showed above for the normal-sized 8×8 chessboard, you can see that the number of squares is equal to the sum of squares from 12 to 82, where 8 is equal to N. So, we can say in more general terms that the sum of squares for a chessboard of size NxN is equal to n^2 + (n-1)^2 + (n-2)^2 + …. (1)^2.

Formula for number of squares on NxN chessboard This is a common summation in math, and is actually equal to n(n + 1)(2n + 1)/6 – so for a chessboard of size nxn, this formula will give you the count of all of the squares on the board.

https://www.programmerinterview.com/puzzles/find-number-of-squares-chessboard/

def sum_squares_iterative(n):
    res = 0
    for i in range(1, n + 1):
        res += i**2
    return res

print(sum_squares_iterative(8))

def sum_squares_recursive(n):
    return (n**2 + sum_squares_recursive(n-1)) if n >= 1 else 0

print(sum_squares_recursive(8))

Fibonacci, iterative and recursive

def fibonacci_iterative(n):
    # 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
    # 0, 1, 2, 3, 4, 5,  6, 7,  8,  9
    a, b = 0, 1
    for _ in range(0, n):
        a, b = b, a + b
    return a

def fibonacci_recursive(n):
    if n > 1:
        return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2)
    return n

Java

Garbage Collection

When are objects move from young generation to the old generation?

On full GC. See: https://codeahoy.com/2017/08/06/basics-of-java-garbage-collection/

Architecture

Describe your current project's architecture.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment