PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches

Quick Overview

This question evaluates proficiency in array manipulation, duplicate handling, and custom sorting logic with attention to time and space complexity, and it falls within the Coding & Algorithms domain.

  • medium
  • Apple
  • Coding & Algorithms
  • Software Engineer

Zero out duplicates and sort an array

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Coding question You are given an integer array `arr` of length about **75**. - The data is **not sorted**. - The array may contain **duplicate values**. - You must **zero out duplicates** and then **sort** the array. - Try to keep **time and space complexity** in mind. - **Do not use** built-in/native sorting utilities (e.g., `sort()`), and do not call library implementations of quicksort/bubblesort/etc. ### Clarification to assume (if not specified) If a value appears multiple times, **keep exactly one occurrence** and replace all other occurrences with **0**. Then sort the entire array in **non-decreasing** order. ### Input - An integer array `arr`. ### Output - Return the transformed array after (1) zeroing duplicate occurrences and (2) sorting. ### Example - Input: `[3, 1, 3, 2, 2]` - After zeroing duplicates (keeping first occurrence): `[3, 1, 0, 2, 0]` - After sorting: `[0, 0, 1, 2, 3]`

Quick Answer: This question evaluates proficiency in array manipulation, duplicate handling, and custom sorting logic with attention to time and space complexity, and it falls within the Coding & Algorithms domain.

You are given an integer array arr. Traverse the array from left to right and keep the first occurrence of each value. Every later occurrence of the same value must be replaced with 0. After that, sort the entire resulting array in non-decreasing order and return it. Do not use built-in sorting utilities such as sort() or sorted(); implement the sorting yourself. Note: if the duplicated value is 0, replacing it with 0 does not change its appearance, but it is still considered a duplicate occurrence.

Constraints

  • 0 <= len(arr) <= 100000; the interview version often uses arrays of around 75 elements
  • -1000000000 <= arr[i] <= 1000000000
  • Keep the first occurrence of each value and replace every later duplicate with 0
  • Do not use built-in/native sorting utilities

Examples

Input: ([3, 1, 3, 2, 2],)

Expected Output: [0, 0, 1, 2, 3]

Explanation: Keep the first 3 and first 2, replace later duplicates with 0 to get [3, 1, 0, 2, 0], then sort.

Input: ([],)

Expected Output: []

Explanation: An empty array stays empty.

Input: ([4],)

Expected Output: [4]

Explanation: A single element has no duplicates, so the array is unchanged after sorting.

Input: ([4, 4, 4, 4],)

Expected Output: [0, 0, 0, 4]

Explanation: Only the first 4 is kept; the other three become 0, then the array is sorted.

Input: ([-1, 3, -1, 2, 3],)

Expected Output: [-1, 0, 0, 2, 3]

Explanation: After zeroing later -1 and 3 values, the array becomes [-1, 3, 0, 2, 0], which sorts to [-1, 0, 0, 2, 3].

Input: ([0, 2, 0, 2, 1],)

Expected Output: [0, 0, 0, 1, 2]

Explanation: Keep the first 0 and first 2. Later 0 and 2 are duplicates and become 0, giving [0, 2, 0, 0, 1], then sort.

Input: ([5, 3, 4, 1, 2],)

Expected Output: [1, 2, 3, 4, 5]

Explanation: All values are unique, so the task reduces to sorting the array without built-in sort utilities.

Solution

def solution(arr):
    if isinstance(arr, tuple) and len(arr) == 1 and isinstance(arr[0], list):
        arr = arr[0]

    seen = set()
    transformed = []

    for value in arr:
        if value in seen:
            transformed.append(0)
        else:
            seen.add(value)
            transformed.append(value)

    temp = [0] * len(transformed)

    def merge_sort(left, right):
        if right - left <= 1:
            return

        mid = (left + right) // 2
        merge_sort(left, mid)
        merge_sort(mid, right)

        i = left
        j = mid
        k = left

        while i < mid and j < right:
            if transformed[i] <= transformed[j]:
                temp[k] = transformed[i]
                i += 1
            else:
                temp[k] = transformed[j]
                j += 1
            k += 1

        while i < mid:
            temp[k] = transformed[i]
            i += 1
            k += 1

        while j < right:
            temp[k] = transformed[j]
            j += 1
            k += 1

        for idx in range(left, right):
            transformed[idx] = temp[idx]

    merge_sort(0, len(transformed))
    return transformed

Time complexity: O(n log n). Space complexity: O(n).

Hints

  1. Use a hash set while scanning from left to right to decide whether a value has already appeared.
  2. Once the array is transformed, implement an O(n log n) sorting algorithm yourself, such as merge sort.
Last updated: Apr 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Compute Earliest Bus Arrival - Apple (medium)
  • Find the Extra Edge - Apple (hard)
  • Rotate a Matrix In Place - Apple (medium)
  • Encode and Rebuild a Binary Tree - Apple (hard)
  • Wrap Matching Substrings in Bold Tags - Apple (medium)