Quick Overview

This question evaluates a candidate's ability in sequence and string processing, including managing state across adjacent elements and handling interface assumptions such as streaming versus in-memory input and case-sensitive comparisons; it belongs to the Coding & Algorithms category and the string-manipulation/data-processing domain.

Implement a uniq-like function

Company: Vanta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Implement a function similar to the Unix `uniq` utility. You are given a sequence of strings representing lines of text in the order they appear in a file. Return a new sequence where **only consecutive duplicate lines are collapsed** into a single line (i.e., remove repeated adjacent duplicates but do not remove duplicates that are separated by different lines). Example: - Input: `["a","a","b","b","b","a","a","c"]` - Output: `["a","b","a","c"]` Clarify with the interviewer: - Whether input is streaming (iterator) or in-memory. - Whether comparison is case-sensitive. - Whether to support options like returning counts per group (e.g., `uniq -c`) or only the collapsed lines.

Overview: This question evaluates a candidate's ability in sequence and string processing, including managing state across adjacent elements and handling interface assumptions such as streaming versus in-memory input and case-sensitive comparisons; it belongs to the Coding & Algorithms category and the string-manipulation/data-processing domain.

Implement a function similar to the Unix `uniq` utility. You are given an in-memory list of strings representing lines of text in the order they appear in a file. Return a new list where only consecutive duplicate lines are collapsed into a single line. Important: only adjacent duplicates should be removed. If the same line appears again later after a different line, it should remain in the result. Assume comparisons are case-sensitive, and you only need to return the collapsed lines (not counts).

Constraints

  • 0 <= len(lines) <= 100000
  • Each element of `lines` is a string
  • Comparison is case-sensitive
  • Return only the collapsed lines; do not return counts

Examples

Input: ["a", "a", "b", "b", "b", "a", "a", "c"]

Expected Output: ["a", "b", "a", "c"]

Explanation: Consecutive duplicates are collapsed, but the later `a` group is kept because it is separated by `b`.

Input: []

Expected Output: []

Explanation: Empty input should return an empty list.

Hints

  1. You only need to compare each line with the most recently kept line.
  2. Be careful with the first element and with empty input.

Community answers

Answer by sasha

public class Unique { public static String unique(String str) { if ( str == null || str .isEmpty()) { return str; } StringBuilder buf = new StringBuilder(); char prev = ' '; for ( char cur : str.toCharArray() ){ if (cur == prev) continue; buf.append(cur); prev = cur; } return buf.toString(); } public static void main(String arg []) { String str = "cvfaaaabfbbbcccaaabbb"; String expected = "cvfabfbcab"; String found = unique(str); System.out.println("Both expected = found " + expected.equals(found)); System.out.println("found =>" + found); } }

Loading coding console...