PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Salesforce

Solve array duplicate flags and binary swaps

Last updated: May 28, 2026

Quick Overview

This question evaluates array and string algorithm skills, specifically hash-based frequency tracking for left/right duplicate detection and reasoning about synchronous substring transformations to determine stabilization time.

  • medium
  • Salesforce
  • Coding & Algorithms
  • Software Engineer

Solve array duplicate flags and binary swaps

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

## Problem 1: Flag duplicates on the left and right You are given an integer array `nums` of length `n`. For each index `i`, determine: 1. **Left-duplicate:** whether `nums[i]` has appeared in any index `< i`. 2. **Right-duplicate:** whether `nums[i]` appears in any index `> i`. Construct two binary strings of length `n`: - `left[i] = '1'` if `nums[i]` appears in `nums[0..i-1]`, else `'0'`. - `right[i] = '1'` if `nums[i]` appears in `nums[i+1..n-1]`, else `'0'`. Return `[left, right]`. **Example** - Input: `nums = [5, 1, 5, 2, 1]` - Output: - `left = "00101"` (the second `5` and the second `1` have appeared on the left) - `right = "10110"` (the first `5` and first `1` appear again on the right) **Constraints (typical):** `1 ≤ n ≤ 2e5`, `nums[i]` fits in 32-bit signed integer. --- ## Problem 2: Synchronous replacement "01" → "10" until stable You are given a binary string `s` (characters are only `'0'` and `'1'`). Each second, **all occurrences** of substring "01" are replaced **simultaneously** with "10". - This is a synchronous update: replacements in the same second do not affect each other. Repeat this process until the string contains **no** "01". Return the number of seconds required. **Example** - Input: `s = "010110"` - Output: `?` (compute the number of synchronous steps until no "01" remains) **Notes:** A direct step-by-step simulation can be too slow for large strings; the intended solution should account for interactions where multiple `'1'`s can “block” each other. **Constraints (typical):** `1 ≤ |s| ≤ 2e5`.

Quick Answer: This question evaluates array and string algorithm skills, specifically hash-based frequency tracking for left/right duplicate detection and reasoning about synchronous substring transformations to determine stabilization time.

Related Interview Questions

  • Solve Two OA Coding Problems - Salesforce (medium)
  • Maximize events attended given date ranges - Salesforce (medium)
  • Implement common data-structure and JS tasks - Salesforce (medium)
  • Minimize operations to reduce integer to zero - Salesforce (medium)
  • Implement an LFU cache with O(1) operations - Salesforce (medium)
Salesforce logo
Salesforce
Dec 17, 2025, 12:00 AM
Software Engineer
Take-home Project
Coding & Algorithms
19
0

Problem 1: Flag duplicates on the left and right

You are given an integer array nums of length n.

For each index i, determine:

  1. Left-duplicate: whether nums[i] has appeared in any index < i .
  2. Right-duplicate: whether nums[i] appears in any index > i .

Construct two binary strings of length n:

  • left[i] = '1' if nums[i] appears in nums[0..i-1] , else '0' .
  • right[i] = '1' if nums[i] appears in nums[i+1..n-1] , else '0' .

Return [left, right].

Example

  • Input: nums = [5, 1, 5, 2, 1]
  • Output:
    • left = "00101" (the second 5 and the second 1 have appeared on the left)
    • right = "10110" (the first 5 and first 1 appear again on the right)

Constraints (typical): 1 ≤ n ≤ 2e5, nums[i] fits in 32-bit signed integer.

Problem 2: Synchronous replacement "01" → "10" until stable

You are given a binary string s (characters are only '0' and '1').

Each second, all occurrences of substring "01" are replaced simultaneously with "10".

  • This is a synchronous update: replacements in the same second do not affect each other.

Repeat this process until the string contains no "01".

Return the number of seconds required.

Example

  • Input: s = "010110"
  • Output: ? (compute the number of synchronous steps until no "01" remains)

Notes: A direct step-by-step simulation can be too slow for large strings; the intended solution should account for interactions where multiple '1's can “block” each other.

Constraints (typical): 1 ≤ |s| ≤ 2e5.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Salesforce•More Software Engineer•Salesforce Software Engineer•Salesforce Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,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.