Valid Discount Coupons Amazon OA 2023

Valid Discount Coupons Amazon OA 2023 Solution

At Amazon’s annual sale, employees are tasked with generating valid discount coupons for loyal customers. However, there are some used/invalid coupons in the mix, and the challenge in this task is to determine whether a given discount coupon is valid or not.

The validity of a discount coupon is determined as follows:

  1. An empty discount coupon is valid.
  2. If a discount coupon A is valid, then a discount coupon C made by adding one character xto both the beginning of A
    and the end of A is also valid (i.e the discount coupon C = xAx is valid).
  3. If two discount coupons A and Bare valid, then the concatenation of B and A is also valid
    (i.e the coupons AB and BA are both valid).

Given n discount coupons, each coupon consisting of only lowercase English characters,

where the i-th discount coupon is denoted discounts[i], determine if each discount coupon is valid or not.

A valid coupon is denoted by 1 in the answer array while an invalid coupon is denoted by 0.


discounts = [‘tabba’; ‘abca’]

Check if this coupon code can be constructed within the rules of a valid coupon.

Checking ‘abba’:

• The empty string is valid per the first rule.

• Under the second rule, the same character can be added to the beginning and end of a valid coupon code.

Add ‘b’ to the beginning and end of the empty string to have ‘bb’, a valid code.

• Using the same rule, ‘a’ is added to the beginning and end of the ‘bb’ coupon string. Again, the string is valid.

The string is valid, so the answer array is [1].

Checking ‘abca’:

• Using rule 2, a letter can be added to both ends of a string without altering its validity.

The ‘a’ added to the beginning and end of ‘bc’ does not change its validity.

• The remaining string ‘Ix’, is not valid. There is no rule allowing the addition of different characters to the ends of a string.

Since the string is invalid, append 0 to the answer array. There are no more strings to test, so return [1,0]

Function Description

Complete the function find ValidDiscountCoupons in the editor below.

find ValidDiscountCoupons has the following parameter:

string discounts[n]: the discount coupons to validate


int[n]: each element i is 1 if the coupon discounts[il is valid and 0 otherwise

Valid Discount Coupons Amazon OA
Valid Discount Coupons Amazon OA Solution


Program: Valid Discount Coupons Amazon OA Solution in Python

def findCoupons(discounts):
	result = []
	for discount in discounts:
		if discount[0] == discount[-1]:
			discount = discount[1:-1]
		result.append(1 if finddiscountPattern(discount)else 0)
	return result
def finddiscountPattern(discount):
	mostleft = 0
	left = 1
	while left < len(discount):
		if discount[mostleft] == discount[left]:
			discount = discount[:mostleft]+discount[left+1:]
			mostleft = 0
			left = 1
			mostleft += 1
			left += 1
	if not len(discount):
		return True
	return False

Program: Valid Discount Coupons Amazon OA Solution in Java

public class Main {
    public static void main(String[] args) {
        List<String> discount = List.of("abba", "abca", "abbacbbc", "aabb", "xaaxybbyzccz", "vaas", "jay");
        // Expected O/P - [1, 0, 1, 1, 1, 0, 0]
        List<Integer> list = findDiscounts(discount);
        // TC - O(n * k), n--> length of the array input & k --> length of each string in input array
        // SC - O(n) - no DS used other than to store response
    private static List<Integer> findDiscounts(List<String> discount) {
        List<Integer> result = new ArrayList<>();
        for (String str : discount) {
            if (str.equals("")) {
                // Condition #1
            } else if (str.length() %2 != 0) {
                // String has to be even length as per given condition, else its invalid coupon
            } else if (isPalindrome(str)) {
                // If string is even length and palindrome, its valid!
            } else if (isPalindrome(findPalindromicPart(str, true)) && 
                      isPalindrome(findPalindromicPart(str, false))) {
                // We can also have concatenation of 2 even palindromes (condition #3) which are valid!
            } else {
        return result;
    private static boolean isPalindrome(String str) {
        if (str == null || str.trim().equals("")) return false;
        int start = 0, end = str.length()-1;
        while (start < end) {
            if (str.charAt(start) != str.charAt(end)) {
                return false;
        return true;
    private static String findPalindromicPart(String str, boolean findLeftPart) {
        // We need to extract palindrome part - it can either be on left side or right side 
        // of the string. So we need to check both.
        // Hence we have this findLeftPart boolean variable
        // We could have simplified this and have 2 functions instead - one for finding 
        // left and other for finding right side palindrome, but that would cause duplicate lines of code
        int start = 0, end = str.length()-1;
        StringBuilder result = new StringBuilder("");
        while (start < end) {
            if (str.charAt(start) == str.charAt(end)) {
            } else if (str.charAt(start) != str.charAt(end)) {
                if (findLeftPart)
        return result.toString() + result.reverse().toString();

Amazon OA 2023 Questions with Solution

  1. Shopping Patterns Solution Amazon OA 2023
  2. Reorder Data in Log Files Solution Amazon OA 2023
  3. Top K Frequent Words Solution Amazon OA 2023
  4. Trees Height Solution Amazon OA SDE 2023
  5. Counting Binary Substrings Amazon OA 2023
  6. Grid Connections Amazon OA 2023
  7. Shipment Imbalance Amazon OA 2023
  8. Max Profit Amazon OA 2023
  9. Find Lowest Price Amazon OA 2023
  10. Decode String Frequency Amazon OA 2023
  11. Simple Cipher Amazon OA 2023
  12. Count Maximum Teams Amazon OA 2023
  13. Minimum Coin Flips Amazon OA 2023
  14. Max Average Stock Price Amazon OA 2023 Solution
  15. Robot Bounded In Circle Amazon OA 2023
  16. Shopping Options Amazon OA 2023 Solution
  17. Fill The Truck Maximum Units on a Truck Amazon OA Solution
  18. Maximize Score After N Operations Number Game Solution Amazon OA 2023
  19. Slowest Key Amazon OA 2023 Solution
  20. Five Star Seller Maximum Average Pass Ratio Amazon OA 2023
  21. Split String Into Unique Primes Amazon OA 2023 Solution
  22. Storage Optimization Amazon OA 2023 Solution
  23. Minimum Difficulty of a Job Schedule Amazon OA 2023 Solution
  24. Autoscale Policy Utilization Check Amazon OA 2023
  25. Optimal Utilization Solution Amazon OA 2023
  26. Merge Two Sorted Lists Solution Amazon OA 2023
  27. Two Sum Unique Pairs Solution Amazon OA 2023
  28. Amazon Music Pairs Amazon OA 2023 Solution
  29. Class Grouping Amazon OA 2023 Solution
  30. Find Max products Amazon OA 2023 Solution
  31. Get encrypted number Amazon OA 2023 Solution
  32. Find Total Imbalance Amazon OA 2023 Solution
  33. Find Total Power Amazon OA 2023 Solution