coding
Salesforce
Amazon
Google

Salesforce Coding: Left-Right Occurrence Binary Strings

Topics:
Hash Table
Two Pointers
Counting
Roles:
Software Engineer
Backend Engineer
Full Stack Engineer
Experience:
Entry Level
Mid Level
Senior

Question Description

Problem summary

You are given an integer array of length n. Produce two binary strings (each length n) that mark whether each element appears elsewhere to its left or to its right.

For index i: the first string has '1' if arr[i] appears at any j < i; the second has '1' if arr[i] appears at any k > i. Return [left_mark_string, right_mark_string] preserving the original order.

What the interviewer expects

Start by clarifying edge cases (empty array, all-unique, all-equal). A brute-force approach checks every pair (O(n^2)). You should show how to optimize to O(n) time with O(n) extra space using hash-based counting and two efficient passes: a left-to-right pass to set left marks using a seen set, and a right-to-left pass (or counts) to set right marks. Discuss trade-offs between one-pass streaming variants and two-pass counting approaches.

Flow in an interview

  • Clarify input/output and constraints
  • Propose brute force and analyze complexity
  • Move to the hash-table / two-pass solution and implement
  • Discuss complexity (time O(n), space O(n)), memory limits, and follow-ups

Skills and signals to demonstrate

You should demonstrate correct use of hash tables (sets or frequency maps), two-pointer/scan thinking, careful indexing/ordering, and clear complexity analysis. Mention streaming or memory-constrained variants and test edge cases to show robustness.

Common Follow-up Questions

  • How would you modify the solution to produce the strings in a single pass (streaming) if you cannot store the entire array?
  • If allowed, how would you change the outputs to report counts (number of occurrences to the left/right) rather than binary flags? What changes in complexity?
  • How would you adapt the approach if the input is a stream and you must update left/right marks online as elements arrive?
  • How does the solution change if values are extremely large or not hashable? Consider using coordinate compression or sorting.
  • Describe how you would test the implementation and which edge cases (e.g., duplicates clustered, interleaved duplicates) are most likely to expose bugs.

Related Questions

1Find first duplicate element in an array (index of first repeated value)
2Return indices of all duplicates using hash-table and two-pass approaches
3Count occurrences of each value to the left or right of every index
4Mark leftmost or rightmost occurrence positions for each value in an array
5Design a streaming algorithm to detect if an element has appeared before (approximate/online)

Explore More Questions

Practice This Question with AI

Get real-time hints, detailed requirements, and insightful analysis of the question.

Left-Right Occurrence Binary Strings — Salesforce Coding | Voker