Solve various LeetCode data-structure questions
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: This collection evaluates proficiency in fundamental data structures and algorithms — including tree traversal and binary search tree reasoning, array manipulation and counting, string/stack validation for parentheses, prefix-sum range queries, and basic dynamic programming — within the Coding & Algorithms domain.
Constraints
- 1 <= len(nums) <= 100000
- -10^9 <= nums[i] <= 10^9
- -10^9 <= target <= 10^9
- Return indices in increasing order
Solution
def target_indices(nums: list[int], target: int) -> list[int]:
# Count elements less than target and equal to target
less = 0
equal = 0
for x in nums:
if x < target:
less += 1
elif x == target:
equal += 1
# The target will occupy indices [less, less + equal - 1] after sorting
if equal == 0:
return []
return list(range(less, less + equal))
Explanation
Time complexity: O(n). Space complexity: O(1) auxiliary (excluding output).
Hints
- Sorting nums and scanning for target works in O(n log n).
- You can avoid sorting: count how many elements are less than target and how many equal target.
- If cLess is the count of elements < target and cEq is the count == target, the answer is the range [cLess, cLess + cEq - 1].