Enphase InterviewQ
Enphase InterviewQ
Example :
Input: nums = [50, 20, 30, 10], target = 80
Output: [0, 2] # Prices 50 + 30 = 80
Solution :
def two_sum(nums, target):
seen = {}
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return []
Test Case
print(two_sum([50, 20, 30, 10], 80)) # Output: [0, 2]
Evaluation Points :
• Handles edge cases (e.g., negative prices, duplicate values).
• Efficiently uses a hash map for O(n) time.
Live Coding Tips : Ask, “What if there are duplicate prices?” (Solution works fine.)
Example :
tracker1 = ResourceTracker()
tracker2 = ResourceTracker()
print(tracker1.increment()) # Output: 1
print(tracker2.increment()) # Output: 2
Solution :
class ResourceTracker:
_total_accesses = 0
@classmethod
def increment(cls):
cls._total_accesses += 1
return cls._total_accesses
Test Case
tracker1 = ResourceTracker()
tracker2 = ResourceTracker()
print(tracker1.increment()) # Output: 1
print(tracker2.increment()) # Output: 2
Evaluation Points :
• Understands class variables vs. instance variables.
• Correctly uses @classmethod .
Live Coding Tips : Ask, “How would you reset the counter?” (Add a @classmethod reset() .)
Solution :
def reverse_file_lines(input_file, output_file):
with open(input_file, 'r') as f:
lines = f.readlines()
with open(output_file, 'w') as f:
f.writelines(reversed(lines))
Test Case
reverse_file_lines('logs.txt', 'reversed_logs.txt')
Evaluation Points :
• Handles file I/O safely (uses context managers).
• Preserves line endings and whitespace.
Live Coding Tips : Ask, “How would you handle a 10GB file?” (Read line by line, store offsets.)
Medium Questions
1. Valid Parentheses
Problem Statement :
You’re building a JSON validator. Given a string containing only (){}[] , determine if the brackets are
balanced.
Examples :
Input: "()[]{}" → Output: True
Input: "(]" → Output: False
Input: "([)]" → Output: False
Solution :
def is_valid(s: str) -> bool:
stack = []
mapping = {')': '(', '}': '{', ']': '['}
for char in s:
if char in mapping:
top = stack.pop() if stack else '#'
if mapping[char] != top:
return False
else:
stack.append(char)
return not stack
Test Cases
print(is_valid("()[]{}")) # True
print(is_valid("([)]")) # False
Evaluation Points :
• Uses a stack to track opening brackets.
• Handles edge cases (empty string, unmatched brackets).
Live Coding Tips : Ask, “How would you extend this to support <angle> brackets?” (Add to
mapping ).
Expected Output :
[ ['CEO'], ['CTO', 'CFO'], ['Eng', 'Acc', 'HR'] ]
Solution :
from collections import deque
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
Test Case
root = TreeNode('CEO',
TreeNode('CTO', TreeNode('Eng')),
TreeNode('CFO', TreeNode('Acc'), TreeNode('HR')))
print(level_order(root))
Evaluation Points :
• Uses BFS with a queue to traverse levels.
• Handles edge cases (empty tree, single-node tree).
Live Coding Tips : Ask, “How would you print levels in reverse?” (Append to result with insert(0,
level) ).
Examples :
Input: [[1, 3], [2, 6], [8, 10]] → Output: [[1, 6], [8, 10]]
Input: [[1, 4], [4, 5]] → Output: [[1, 5]]
Solution :
def merge(intervals: list[list[int]]) -> list[list[int]]:
intervals.sort(key=lambda x: x[0])
merged = []
for interval in intervals:
if not merged or merged[-1][1] < interval[0]:
merged.append(interval)
else:
merged[-1][1] = max(merged[-1][1], interval[1])
return merged
Test Cases
print(merge([[1, 3], [2, 6], [8, 10]])) # [[1, 6], [8, 10]]
print(merge([[1, 4], [4, 5]])) # [[1, 5]]
Evaluation Points :
• Sorts intervals first for O(n log n) time.
• Handles edge cases (empty list, non-overlapping intervals).
Live Coding Tips : Ask, “What if intervals are unsorted?” (Clarify sorting is allowed).
Difficult Questions
1. LRU Cache
Problem Statement :
You’re designing a caching system for a web server. Implement an LRU (Least Recently Used) cache
with:
• get(key) : Return the value if exists, otherwise -1 .
• put(key, value) : Add/update the value. Evict the least recently used item if capacity is exceeded.
Constraints :
• Operations must be O(1) time.
• Capacity is fixed at initialization.
Example :
cache = LRUCache(2)
cache.put(1, 10) # Cache: {1=10}
cache.put(2, 20) # Cache: {1=10, 2=20}
cache.get(1) # Returns 10, Cache: {2=20, 1=10}
cache.put(3, 30) # Evicts 2, Cache: {1=10, 3=30}
Solution :
from collections import OrderedDict
class LRUCache:
def __init__(self, capacity: int):
self.cache = OrderedDict()
self.capacity = capacity
Live Coding Tips : Ask, “How would you handle thread safety?” (Add locks in a real system).
Examples :
Input: num_courses = 2, prerequisites = [[1, 0]] → Output: True
Input: num_courses = 2, prerequisites = [[1, 0], [0, 1]] → Output: False (cycle)
Test Cases
print(can_finish(2, [[1, 0]])) # True
print(can_finish(2, [[1, 0], [0, 1]])) # False
Evaluation Points :
• Detects cycles using Kahn’s algorithm (BFS + in-degree counting).
• Handles edge cases (no prerequisites, disconnected graph).
Live Coding Tips : Ask, “What if the graph is disconnected?” (Still works—counts reachable
nodes).
Examples :
Input: [10, 9, 2, 5, 3, 7, 101, 18] → Output: 4 (e.g., [2, 3, 7, 101])
Input: [0, 1, 0, 3, 2, 3] → Output: 4 (e.g., [0, 1, 2, 3])
Test Cases
print(length_of_LIS([10, 9, 2, 5, 3, 7, 101, 18])) # 4
print(length_of_LIS([0, 1, 0, 3, 2, 3])) # 4
Live Coding Tips : Ask, “Can you reconstruct the subsequence?” (Track predecessors in DP array).