Panda Guru LogoPanda
Guru

Amazon | SDE 2 | Online Assessment

Round 1

Questions:

Question

A team of developers at Amazon is working on a feature that checks the strength of a password as it is set by a user. Analysts found that people use their personal information in passwords, which is insecure. The feature searches for the presence of a reference string as a subsequence of any permutation of the password. If any permutation contains the reference string as a subsequence, then the feature determines the minimum cost to remove characters from password so that no permutation contains the string reference as a subsequence. The removal of any character has a cost given in an array, cost, with 26 elements that represent the cost to replace 'a' (cost[0]) through 'Z' (cost[25]). Given two strings password and reference, determine the minimum total cost to remove characters from password so that no permutation contains the string reference as a subsequence.

Note:

Example Suppose password = "adefgh", reference = "hf", and cost = [1, 0, 0, 2, 4, 4, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]. Some possible removals from password so that no permutation contains the string reference as a subsequence are:

image

Since removing either h or f will remove the existence of reference as a subsequence so we optimally choose to remove 'h' (Case 1), therefore the minimum cost is 1.

Function Description Complete the function minCost in the editor below.

minCost has the following parameters:

Returns long int: the minimum cost to remove characters from password such that no permutation contains the reference string as a subsequence

Constraints

Sample: image

Sample Output: 3

Explanation: An optimal move is to remove the occurrences of the letter 'c' resulting in a modified password = "abdbb". The total cost of removal is = 1*3 = 3.


Question

Developers at Amazon are working on a text generation utility for one of their new products. Currently, the utility generates only special strings. A string is special if there are no matching adjacent characters. Given a string s of length n, generate a special string of length n that is lexicographically greater than s. If multiple such special strings are possible, then return the lexicographically smallest string among them.

Notes:

Important Considerations:

Example Suppose s = "abbd". Some of the special strings that are lexicographically greater than s are shown.

image

The lexicographically smallest special string that is greater than "abbd" is "abca".

Function Description Complete the function getSpecialString in the editor below. getSpecialString has the following parameter:

Returns string: the lexicographically smallest string that is greater than s. If no such special string exists, return "-1".

Constraints:

Sample Sample Input For Custom Testing

STDIN FUNCTION abccde → s = "abccde"

Sample Output: abcdab

Explanation Some of the special strings that are lexicographically greater than s are "abcdde", "abcdab", "abcdbc".