Problem Statement: You are given an implementation of a function which accepts as input a non-empty zero-indexed array A consisting of N integers.
The function works slowly on large input data and the goal is to optimize it so as to achieve better time and/or space complexity. The optimized function should return the same result as the given implementation for every input that satisfies the assumptions.
Examples:
(Note: Candidates must analyze the hidden provided code and write an optimized version that yields the same mathematical result).
Problem Statement: Write a function that, given a string S consisting of N lowercase English letters, returns the length of the longest substring in which every letter occurs an even number of times. A substring is defined as a contiguous segment of a string. If no such substring exists, return 0.
Examples:
Based on the exact inputs and outputs provided (A = [4, 6, 2, 2, 6, 6, 1] returning 4), this is the classic "Count Distinct Elements" problem. The unoptimized hidden code is likely using a nested loop O(N^2) approach to check for duplicates, which is why an array of 50,000 identical elements times out. We need to optimize this to O(N) or O(N \log N).
We can solve this in two optimal ways:
Given that N = 50,000, the O(N) Hash Set approach is extremely fast and the most idiomatic way to solve this in modern C++.
Code Implementation (C++)
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
int getDistinctCount(const vector<int>& a) {
unordered_set<int> distinct_elements;
for (int num : a) {
distinct_elements.insert(num);
}
// The size of the set is the number of unique elements
return distinct_elements.size();
}We need to find the longest substring where every character appears an even number of times. Checking every possible substring would take O(N^3) or O(N^2), which is too slow.
Instead, we can track the "parity" (even or odd count) of all 26 lowercase letters using a Bitmask. Since there are 26 letters, we can use a 32-bit integer where each bit represents a letter ('a' is bit 0, 'b' is bit 1, etc.).
Code Implementation (C++)
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;
int longestEvenSubstring(const string& s) {
unordered_map<int, int> mask_first_index;
int current_mask = 0;
int max_length = 0;
mask_first_index[0] = -1;
for (int i = 0; i < s.length(); ++i) {
int bit_pos = s[i] - 'a';
// Toggle the bit using XOR
current_mask ^= (1 << bit_pos);
if (mask_first_index.find(current_mask) != mask_first_index.end()) {
int previous_index = mask_first_index[current_mask];
max_length = max(max_length, i - previous_index);
} else {
mask_first_index[current_mask] = i;
}
}
return max_length;
}