SAP MM Interview Questions - Solving Challenge
#1
Hey everyone,

I found this great list of SAP MM (Materials Management) interview questions online. Let's tackle one together! Here's a problem for you:
Problem Statement:

You are given an array of integers representing the stock prices of a company over a period of time. Your task is to find the best time to buy and sell the stock to maximize profit. You can only make one transaction (i.e., buy one and sell one share of the stock), and you cannot engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

Write a Python function to solve this problem.
Code:
def max_profit(prices):
    # Your code here
    pass

# Example usage:
prices = [7, 1, 5, 3, 6, 4]
print(max_profit(prices))  # Expected output: 5 (Buy at 1 and sell at 6)

Feel free to discuss your approach, share your code, or ask for help if you're stuck. Let's learn and improve our problem-solving skills together!

Reference to interview questions: SAP MM Interview Questions and Answers.
Reply
#2
Instructions unclear.

Only one buy and one sell in all ?
Then what is meant by "you must sell the stock before you buy again"

If only one buy and sell then:
Find leftmost smallest and rightmost largest elements.
Take the difference.

Untested Python like Pseudo code. Quite likely buggy.

^Totally flawed logic. :-)

Try 2:
Need to traverse the stock prices in reverse.
Didn't know much Python. So googled for syntax, as i wrote it.
Has similarities to the previous problem posed.

Code:
#!python

import random


def maxprofit (L):
    # max profit init
    M = 0
    # For each Sell price starting from last element to all but first
    for i in range (L-1, 1, -1):
        j = i-1
        # for each previous element
        while j >= 0:
            # find difference
            d = x[i] - x[j]
            # if more than current max_profit, note this
            if (d > M):
                M = d
                S = i
                B = j
            j=j-1
    return (M, S, B)


if __name__ == '__main__':

    # Test data generation values
    # Create an array of N stock prices with values ranging from 1 to R-1
    N = 20
    R = 100

    # stock price list
    x = [random.randint(1, R) for i in range(N)]
    L = len(x)

    # Max Profit, Index of Sell, Index of Buy in x
    [M, S, B] = maxprofit (L)

    print (x)
    print ("max profit = ", M)
    print (" buy @", x[B], " on day ", B+1)
    print ("sell @", x[S], " on day ", S+1)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Manual Testing Interview Questions Avantika_Sharmaa24 2 6,372 Sep 15, 2023, 06:39 am
Last Post: gulshan212
  Need help with statistics interview question Avantika_Sharmaa24 2 9,181 Aug 25, 2023, 10:19 am
Last Post: gulshan212
  Trouble with Deep Learning Interview Questions Avantika_Sharmaa24 3 9,402 Jul 13, 2023, 12:50 pm
Last Post: gulshan212
  Need help with SQL query interview questions Avantika_Sharmaa24 1 6,370 Jul 11, 2023, 08:12 am
Last Post: gulshan212
  Need help understanding Informatica Interview Questions Avantika_Sharmaa24 1 7,472 Jun 27, 2023, 06:59 am
Last Post: gulshan212



Users browsing this thread: 1 Guest(s)