Panda Guru LogoPanda
Guru

Amazon OA - SDE 2 2024

Round 1

Questions:

Question 1: Minimum Value Calculation by Replacing '!' in a Binary String

Problem:

You are given a string consisting of '0', '1', and '!', where '!' represents a character that can be replaced with either '0' or '1'. Additionally, you are given two integers x and y.

Your task is to replace all '!' characters in such a way that you minimize the calculated value based on the following rules:

  1. For every adjacent pair of characters:
    • If the pair is '01', the contribution to the total is x.
    • If the pair is '10', the contribution to the total is y.

Example:

  1. Input:

    • String: '01!0'
    • x = 2
    • y = 3

    Output: 8

    Explanation:

    • Replace '!' with '0' to get '0100':
      • Pairs: ('01', '10') → Contribution: (2 * 1 + 2 * 3 = 8)
    • Replace '!' with '1' to get '0110':
      • Pairs: ('01', '10') → Contribution: (2 * 2 + 2 * 3 = 10)
    • The minimum contribution is (8).
  2. Input:

    • String: '!0!1'
    • x = 3
    • y = 4

    Output: 7

    Explanation:

    • Replace the first '!' with '1' and the second '!' with '0' to get '10' and '01':
      • Pairs: ('10', '01') → Contribution: (3 + 4 = 7)

Constraints:

Candidate's Approach

No approach provided.

Interviewer's Feedback

No feedback provided.


Question 2: Find First Index Where Prefix Sum Becomes Non-Positive

Problem:

You are given an array of integers representing inventory stock. Your task is to find the first index where the prefix sum of the array becomes non-positive (i.e., less than or equal to 0). The prefix sum is the cumulative sum of elements from the start of the array to the current element. If no such index exists, return -1.

Example:

Constraints:

Candidate's Approach

No approach provided.

Interviewer's Feedback

No feedback provided.