r/InterviewDB • u/interviewdb • 13h ago
AMEX (American Express) SWE Codility Assessment Questions
Sharing some info about the American Express SWE Codility OA from a candidate who took it recently.
Codility Platform Features and Things to Watch Out For
Interface: The problem description is on the left, the code editor is on the right, and there are Custom Test Cases at the bottom for running tests.
Scoring mechanism: Codility immediately provides a score after submission, typically divided into two parts: Correctness and Performance (efficiency/time complexity). Edge cases (empty arrays, extremely large values) and O(N2) solutions will very likely cause some Performance test cases to fail.
Environment: No auto-complete, no external libraries, and you have to manually write basic syntax (for example, using dict instead of collections.Counter in Python).
Language: I chose Python because it's relatively quick to code in.
Below were the questions I got in the OA.
Question 1: Subarray Sums Divisible by K — Corresponds to LeetCode 974
Problem description: Given an integer array nums and an integer k, return the number of non-empty contiguous subarrays whose sum is divisible by k.
Example:
text Input: nums = [4,5,0,-2,-3,1], k = 5 Output: 7
Solution approach (Prefix Sum + Modular Congruence Theorem + Hash Table):
When you see "subarray sum," the first thing that should come to mind is prefix sums. Suppose the prefix sum array is P. Then the sum of the subarray nums[i..j] can be expressed as P[j] - P[i-1].
The problem requires (P[j] - P[i-1]) % k == 0. According to the modular congruence theorem, this means P[j] % k == P[i-1] % k.
Therefore, we only need to iterate through the array, calculate the current prefix sum, and take its modulo. If the same remainder has appeared before, it means the sum of the subarray in between is divisible by k.
Use a hash table to record how many times each remainder has appeared. Note: If the remainder is negative (the array may contain negative numbers), the modulo operation needs some handling: mod = (current_sum % k + k) % k.
Initialize the hash table with {0: 1}, representing one occurrence of a prefix sum of 0.
Complexity:
Time complexity: O(N). You must achieve O(N); otherwise, Codility's Performance tests will fail.
Space complexity: O(min(N, K)).
Reference code:
def solution(A, K):
count = {0: 1}
current_sum = 0
result = 0
for num in A:
current_sum += num
mod = (current_sum % K + K) % K # Handle modulo with negative numbers
if mod in count:
result += count[mod]
count[mod] = count.get(mod, 0) + 1
return resultQuestion 2: Container With Most Water — LeetCode 11
Problem description: Given an integer array height of length n, there are n vertical lines. Find two lines that, together with the x-axis, form a container that can hold the most water. Return the maximum amount of water.
Solution approach (Two Pointers):
A classic greedy two-pointer problem. Initialize two pointers, left = 0 and right = len(height) - 1.
The container's capacity is determined by the shorter vertical line, with the formula area = min(height[left], height[right]) * (right - left).
Calculate the current area and update the maximum area max_area.
Pointer movement strategy: Always move the pointer with the smaller height (if the left one is shorter, left += 1; if the right one is shorter, right -= 1).
Reason: If you move the taller pointer, the container's width decreases while its height is still limited by the shorter pointer, so the area cannot increase. Only by moving the shorter pointer is there a chance of encountering a taller line that compensates for the decrease in width.
Complexity:
Time complexity: O(N). The two-pointer approach only requires one traversal. If you use nested loops with O(N2,) Codility will directly give you a TLE (Time Limit Exceeded).
Space complexity: O(1).
Reference code:
def solution(H):
left, right = 0, len(H) - 1
max_area = 0
while left < right:
current_area = min(H[left], H[right]) * (right - left)
max_area = max(max_area, current_area)
if H[left] < H[right]:
left += 1
else:
right -= 1
return max_areaInterview Impressions and Advice
Codility pitfalls: Make sure to write and run a few Custom Test Cases yourself before submitting. For the first question, definitely test a case containing negative numbers (such as [-1, 2, -4, 3], K=3). Many people get their % logic wrong here. For the second question, test the edge case where there are only two elements.
Don't use overly complicated libraries: Codility's default Python environment is fairly bare-bones. You can use import collections, but to avoid potential execution issues, sticking with the built-in dict is the safest option.
Results: If both Correctness and Performance are all green after submitting on Codility, you've pretty much secured a pass on the OA. I received notification about the next step approximately one week after completing it.