[Amazon] INTERN ONLINE ASSESSMENT 2024-12-28

1. Code Question 1

Amazon developers are building a prototype for a regex generator for the given strings. For the prototype version, the regex consists of uppercase English letters, '[' and ']'. Here, a string of characters enclosed in square brackets '[' and ']' matches any of the characters in that string. Also, the string of characters enclosed within '[' and ']' consists of uppercase English letters only with no repetition.

For example:

  • "[ABBR]ABC" is not a valid regex as B occurs twice between square brackets.
  • "["ABC", "A[A[", "]" and "]ABC" are also not valid regexes as brackets must contain some string, and brackets should also form a balanced bracket sequence.
  • "[ABC][BCA]" is a valid regex and matches with "BBCA" and "ABCA", but not with "ABBCA".
  • "[ABZ][Q]" is a valid regex and matches with "AQ", "BQ", "CQ", "ZQ" but not with "DQ" or "ZC".
  • "[A][C][D]" is a valid regex and matches only with the string "ABCD".

Given 3 strings [b]x, y, and z of length n, find the longest regex which matches both the strings x and y but does not match with the string z. If no regex exists that satisfies the multiple such regex exist output the lexicographically smallest one.

  • The length of a regex is the number of characters in it, i.e., including uppercase English letters and '[' and ']'.
  • A string p is lexicographically smaller than string q, if p is a prefix of q, is not equal to q, or there exists i such that p_i < q_i and for all j < i, it is satisfied that p_j = q_j. Note that while comparing p_j and q_j, we consider their ASCII values, i.e., '[' and ']' are greater than any uppercase English letters. For example, "ABC" is lexicographically smaller than "BCD" and "ABCDE" but larger than "AAC" and "AACDE".
  • The answer string, which is to be returned can turn out to be in the order of 10^6 for larger n's.

Example:
x = "AB", y = "BD", z = "CD"
Regex "[ABCDEFGHIJKLMNOPQRSTUVWXYZ][ABCDEFGHIJKLMNOPQRSTUVWXYZ]" matches with strings "AB" and "BD", but not with string "CD".

Function Description
Complete the function findLongestRegex in the editor below.

findLongestRegex has the following parameters:

  • x: a string
  • y: a string
  • z: a string

Returns
string: the longest lexicographically smallest regex matching x and y but not z, if no such regex exists return -1 as a string.


2. Code Question 2

A team of financial analysts at Amazon closely monitors revenue generated by a newly launched product. They classify a period of one or more consecutive days as a stable-growth period if the revenue generated by the product takes no more than k distinct values over that period.

Given an array revenues of size n, that represents the revenues generated by the new product on n consecutive days, and an integer k, determine the total number of stable growth periods over the n days. Since the answer can be large, return it modulo (10^9 + 7).

Example

Suppose n = 3, k = 1, revenues = [1, 2, 1].


Function Description

Complete the getStablePeriodsCount function in the editor below.

getStablePeriodsCount has the following parameters:

  • int revenues[n]: the revenues generated by the new product over n days
  • int k: the maximum number of distinct values in a stable growth period

Returns

  • int: the number of stable growth periods of the product over n days, modulo (10^9 + 7)

Constraints

  • 1 ≤ n ≤ 10^5
  • 1 ≤ kn
  • -10^9 ≤ revenues[i] ≤ 10^9

Table Explanation:

Indices of daysPeriodsDistinct Values
[0][1]1
[0, 1][1, 2]2
[0, 1, 2][1, 2]2
[1][2]1
[1, 2][2, 1]2
[2][1]1

There are three periods with k = 1 or fewer distinct values. The number of stable growth periods is therefore 3.


我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。

We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.

Leave a Reply

Your email address will not be published. Required fields are marked *