FastPrepStreaming Palindrome Checker

Streaming Palindrome Checker

Bloomberg LP logoBloomberg LP● HardNEW GRADONSITE INTERVIEW
Learn

Problem statement

Process an append-only stream of lowercase characters. The stream starts empty, and each row of operations is one of:

  • ["track", character]: append the one-character string character to the stream.
  • ["isPalindrome"]: report whether the entire stream currently reads the same forward and backward.

Return the boolean answers to the isPalindrome operations in encounter order. The empty stream is a palindrome.

Each isPalindrome query must run in O(1) time. The complete operation sequence must be processed exactly, without probabilistic hashing.

Function

streamPalindrome(operations: String[][]) → boolean[]

Examples

Example 1

operations = [["track","a"],["track","b"],["track","c"],["track","b"],["track","a"],["isPalindrome"]]return = [true]

The tracked stream is abcba, which is a palindrome.

Example 2

operations = [["isPalindrome"],["track","a"],["isPalindrome"],["track","b"],["isPalindrome"],["track","a"],["isPalindrome"]]return = [true,true,false,true]

The queried streams are empty, a, ab, and aba.

Example 3

operations = [["track","a"],["track","b"],["track","b"],["isPalindrome"],["track","a"],["isPalindrome"],["track","c"],["isPalindrome"]]return = [false,true,false]

The queried streams are abb, abba, and abbac.

Constraints

  • 1 <= operations.length <= 200000.
  • Every row is exactly ["track", character] or ["isPalindrome"].
  • Every tracked character is one lowercase English letter.
  • At least one isPalindrome operation occurs.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public boolean[] streamPalindrome(String[][] operations) {
    // Write your code here.
}
operations[["track","a"],["track","b"],["track","c"],["track","b"],["track","a"],["isPalindrome"]]
expected[true]
Checking account…