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.
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.
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.
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.
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.