Solve mixed coding tasks from interviews
Company: Apple
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates binary tree traversal and leaf detection, calendrical date arithmetic and palindrome checking, discrete-time edge-detection/stateful sequence processing, and implementation of comparison-based sorting; category/domain: Coding & Algorithms. It is commonly asked to assess correctness and edge-case handling (e.g.
Part 1: Binary Tree Leaves
Constraints
- 0 <= len(level_order) <= 10^4
- Each element is either an integer or None
- If the list is empty or the root is None, the tree is empty
Examples
Input: ([1, 2, 3, 4, 5, None, 6],)
Expected Output: [4, 5, 6]
Explanation: The leaf nodes are 4, 5, and 6, read from left to right.
Input: ([1],)
Expected Output: [1]
Explanation: A single-node tree has one leaf: the root itself.
Input: ([],)
Expected Output: []
Explanation: An empty tree has no leaves.
Input: ([1, None, 2, 3],)
Expected Output: [3]
Explanation: The only leaf is 3.
Input: ([1, 2, 3, None, None, 4, 5],)
Expected Output: [2, 4, 5]
Explanation: Node 2 is a leaf, and nodes 4 and 5 are leaves in the right subtree.
Hints
- A node is a leaf only when both its left and right children are missing.
- If you reconstruct the tree from level order, visit the left subtree before the right subtree to preserve left-to-right leaf order.
Part 2: Previous Palindrome Date
Constraints
- The input date is a valid Gregorian date from 0001-01-01 to 9999-12-31
- Leap years follow the Gregorian rule: divisible by 400, or divisible by 4 but not by 100
- Return the answer strictly earlier than the given date
Examples
Input: ('2021-12-03',)
Expected Output: '2021-12-02'
Explanation: 20211202 is a palindrome and is the closest earlier valid date.
Input: ('2021-12-02',)
Expected Output: '2020-02-02'
Explanation: The given date itself is palindromic, but the answer must be strictly earlier.
Input: ('2015-01-01',)
Expected Output: '2011-11-02'
Explanation: Years 2014, 2013, and 2012 produce invalid months; 2011 gives the valid palindrome date 2011-11-02.
Input: ('9220-03-01',)
Expected Output: '9220-02-29'
Explanation: 9220-02-29 is palindromic and valid because 9220 is a leap year.
Input: ('0101-10-10',)
Expected Output: None
Explanation: 0101-10-10 is the earliest valid palindromic date in the supported year range, so no earlier one exists.
Hints
- If YYYYMMDD is a palindrome, then MMDD is completely determined by the 4 digits of YYYY.
- Generate at most one candidate per year by reversing the 4-digit year, then validate month and day.
Part 3: Rising Edge Detector
Constraints
- 0 <= len(x) <= 10^5
- Each x[i] is either 0 or 1
Examples
Input: ([0, 0, 1, 1, 0, 1],)
Expected Output: [0, 0, 1, 0, 0, 1]
Explanation: Rising edges happen at indices 2 and 5.
Input: ([1, 1, 1],)
Expected Output: [0, 0, 0]
Explanation: There is no 0-to-1 transition.
Input: ([0, 1, 0, 1, 0],)
Expected Output: [0, 1, 0, 1, 0]
Explanation: Rising edges occur at indices 1 and 3.
Input: ([],)
Expected Output: []
Explanation: An empty signal produces an empty output.
Input: ([0],)
Expected Output: [0]
Explanation: The first position is always 0 by definition.
Hints
- Each output value depends only on the current sample and the previous sample.
- Handle index 0 separately, and do not forget the empty-array case.
Part 4: Sort Integers in Ascending Order
Constraints
- 0 <= len(nums) <= 10^5
- Each integer fits in the standard 32-bit signed range
Examples
Input: ([5, 2, 4, 1, 3],)
Expected Output: [1, 2, 3, 4, 5]
Explanation: A basic unsorted input.
Input: ([],)
Expected Output: []
Explanation: An empty list is already sorted.
Input: ([7],)
Expected Output: [7]
Explanation: A single element is already sorted.
Input: ([3, -1, 3, 2, 0],)
Expected Output: [-1, 0, 2, 3, 3]
Explanation: The list contains duplicates and negative values.
Input: ([5, 4, 3, 2, 1],)
Expected Output: [1, 2, 3, 4, 5]
Explanation: A reverse-sorted input should also be handled correctly.
Hints
- A divide-and-conquer approach works well here.
- After sorting the left and right halves, merge them by repeatedly taking the smaller front element.