1. Correctness Analysis
Check for:
- Edge cases handling (empty input, null, single element)
- Boundary conditions (array indices, loop termination)
- Logic errors in algorithm implementation
- Test case coverage (basic, edge, corner cases)
Common edge cases:
- Empty arrays/strings:
[], "" - Null inputs:
null, None - Single element:
[1], "a" - Duplicates:
[1,1,1] - Negative numbers:
[-1, -5] - Large inputs: Test time/space limits
2. Time & Space Complexity
Analyze and verify:
- Time complexity: Count operations relative to input size
- Space complexity: Count auxiliary space used
- Compare against optimal solution
Provide:
```
Current: O(nΒ²) time, O(1) space
Optimal: O(n) time, O(n) space using HashMap
Trade-off: Use extra space for better time complexity
```
Complexity Reference:
- O(1): Direct access
- O(log n): Binary search, balanced tree
- O(n): Single pass, linear scan
- O(n log n): Efficient sorting, divide-and-conquer
- O(nΒ²): Nested loops
- O(2βΏ): Exponential (backtracking, brute force)
3. Code Quality - Java
Java Best Practices:
- Use appropriate data structures (
ArrayList, HashMap, HashSet) - Follow naming conventions (camelCase for methods/variables)
- Handle null checks and validation
- Use generics properly (
List not raw types) - Prefer interfaces over implementations (
List<> not ArrayList<>)
Java Anti-patterns to flag:
```java
// Bad: Raw types
ArrayList list = new ArrayList();
// Good: Generics
List list = new ArrayList<>();
// Bad: Manual array copying
for (int i = 0; i < arr.length; i++) { ... }
// Good: Built-in methods
Arrays.copyOf(arr, arr.length);
// Bad: String concatenation in loop
String s = "";
for (String str : list) { s += str; }
// Good: StringBuilder
StringBuilder sb = new StringBuilder();
for (String str : list) { sb.append(str); }
```
Check for:
- Integer overflow: Suggest
long when needed - Proper exception handling
- Memory leaks (unclosed resources)
- Thread safety if applicable
4. Code Quality - Python
Python Best Practices:
- Use Pythonic idioms (list comprehensions, enumerate, zip)
- Follow PEP 8 style guidelines
- Use appropriate data structures (
set, dict, deque) - Leverage built-in functions
Python Anti-patterns to flag:
```python
# Bad: Manual index tracking
for i in range(len(arr)):
print(i, arr[i])
# Good: enumerate
for i, val in enumerate(arr):
print(i, val)
# Bad: Building list with append in loop
result = []
for x in arr:
result.append(x * 2)
# Good: List comprehension
result = [x * 2 for x in arr]
# Bad: Multiple membership checks
if x == 'a' or x == 'b' or x == 'c':
# Good: Use set or tuple
if x in {'a', 'b', 'c'}:
```
Check for:
- Use of appropriate collections (
collections.defaultdict, Counter) - Generator expressions for memory efficiency
- Proper use of
None checks - Type hints for clarity (optional but helpful)
5. Algorithm Optimization
Suggest improvements for:
- Unnecessary nested loops β Use HashMap for O(n)
- Repeated calculations β Use memoization/DP
- Redundant sorting β Use heap or quick select
- Multiple passes β Combine into single pass
- Extra space usage β In-place modifications
Pattern Recognition:
- Two Sum pattern β Use HashMap
- Sliding Window β Two pointers
- Subarray sum β Prefix sum
- Longest substring β Sliding window + HashMap
- Tree traversal β DFS/BFS with proper data structure
6. Comparison: Java vs Python
When comparing implementations:
Java strengths:
- Explicit types catch errors early
- Better for performance-critical code
- Clear data structure usage
Python strengths:
- More concise and readable
- Rich standard library (collections, itertools)
- Better for rapid prototyping
Flag inconsistencies:
- Different algorithms used (should be same approach)
- Different time/space complexity
- Missing edge case handling in one version
7. Review Output Format
Structure your review as:
```markdown