Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New interview questions #30

Open
cibic89 opened this issue Apr 7, 2022 · 0 comments
Open

New interview questions #30

cibic89 opened this issue Apr 7, 2022 · 0 comments

Comments

@cibic89
Copy link

cibic89 commented Apr 7, 2022

Fibonacci

# Iterative solution
def fibonacci(n):
    # special case
    if n == 1:
        return [0]
    # initialize the array
    arr = [0, 1]
    # and populate it, remember to skip the 2 values that are already
present
    for i in range(2, n):
        arr.append(arr[-1]+arr[-2])
    return arr


# Recursive solution
def fibonacci(n):
    # handle special cases
    print(n)
    if n < 2:
        return [0]
    if n < 3:
        return [0, 1]
    # normal case as n decreases from original value to 2
    arr = fibonacci(n-1)
    arr.append(arr[n-2] + arr[n-3])
    return arr

Find the sequence Sum

def getSequenceSum(i, j, k, x):
    cnt_inc = (j - i) // x + 1
    cnt_dec = (j - k) // x + 1
    inc_sum = (cnt_inc * (i + j)) // 2
    dec_sum = (cnt_dec * (j + k)) // 2
    tot_sum = inc_sum + dec_sum - j
    return tot_sum

Maximizing Profit from Stocks

def maximumProfit(prices):
    m = prices.pop()
    maxsum = 0
    arrsum = 0
    for p in reversed(prices):
        m = max(m, p)
        maxsum+=m
        arrsum+=p
    return maxsum-arrsum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant