Determine max remainders and anagram after deletions
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Take-home Project
Quick Answer: In the Coding & Algorithms domain, this two-part question evaluates modular arithmetic and combinatorial reasoning for maximizing distinct array remainders and frequency-vector manipulation for determining anagram feasibility after selective character deletions, while also testing algorithm design and time/space complexity analysis.
Maximum Distinct Remainders
Constraints
- 0 <= n
- A[i] > 0 for all i
- B[i] may be any integer, so C[i] can be any value in [0, A[i]-1]
Examples
Input: ([2, 7, 11, 15],)
Expected Output: 4
Explanation: Sorted [2,7,11,15]: claim 0 (from 2), 1 (from 7), 2 (from 11), 3 (from 15) -> 4 distinct values.
Input: ([1, 1, 1],)
Expected Output: 1
Explanation: Each A[i]=1 forces C[i] in [0,0], so only value 0 is ever possible -> 1 distinct value.
Input: ([5],)
Expected Output: 1
Explanation: Single index can claim exactly one value (e.g. 0). Max distinct = 1.
Input: ([],)
Expected Output: 0
Explanation: Empty array yields no values -> 0 distinct.
Input: ([1, 2, 2, 3],)
Expected Output: 3
Explanation: Claim 0 (from 1), 1 (from 2), the second 2 can't add a new value below 2, 2 (from 3) -> distinct {0,1,2} = 3.
Input: ([3, 3, 3, 3],)
Expected Output: 3
Explanation: Each range is [0,2]; you can claim 0,1,2 but the fourth index has no new reachable value -> 3.
Input: ([1, 1, 5],)
Expected Output: 2
Explanation: First 1 claims 0; second 1 can't reach a new value; 5 claims 1 -> {0,1} = 2.
Hints
- Since B[i] is free, C[i] = B[i] mod A[i] can equal any value in [0, A[i]-1]. Reframe: each index lets you pick one value from a range starting at 0.
- To maximize distinct picks, process the smaller ranges first so they grab the small values they alone can reach, leaving larger ranges for larger values.
- Sort A ascending; keep a counter cur=0; for each a, if cur < a claim value cur and increment cur. The answer is cur.
Anagram After Deleting One Letter Type From Each String
Constraints
- s and t contain only lowercase English letters
- You delete from at most one letter type in each string
- Deleting zero occurrences is allowed, so 'no deletion' is always an option
Examples
Input: ("a", "a")
Expected Output: True
Explanation: Already anagrams; delete zero of any letter.
Input: ("abc", "abc")
Expected Output: True
Explanation: Already identical frequency vectors.
Input: ("aab", "ab")
Expected Output: True
Explanation: Delete one 'a' from s -> {a:1,b:1} = t.
Input: ("aabb", "ab")
Expected Output: False
Explanation: s is heavier on both a and b; only one letter type can be deleted in s, so you cannot fix both.
Input: ("abc", "abd")
Expected Output: True
Explanation: Delete 'c' from s and 'd' from t; both become {a:1,b:1} -> anagrams.
Input: ("", "")
Expected Output: True
Explanation: Two empty strings are trivially anagrams.
Input: ("a", "")
Expected Output: True
Explanation: Delete the single 'a' from s -> empty = t.
Input: ("ab", "cd")
Expected Output: False
Explanation: Four mismatched letters (a,b on s side; c,d on t side); only one deletion-letter per string can repair at most one per side.
Input: ("aab", "abb")
Expected Output: True
Explanation: s heavier on a, t heavier on b; delete one 'a' in s and one 'b' in t -> both {a:1,b:1}.
Input: ("xyz", "x")
Expected Output: False
Explanation: s is heavier on both y and z, but you can only delete one letter type from s.
Hints
- Deleting any number of one chosen letter only lets you DECREASE that single letter's count in each string; you can never increase a count.
- Compare frequency vectors. Any letter where s and t already differ must be repaired by deleting from whichever side is heavier.
- You get one deletion-letter in s and one in t. So you can repair at most one letter where s is heavier (delete in s) and at most one letter where t is heavier (delete in t). All other letters must already match.