Problem · Design

String Deque with Constant-Time Operations

Learn this problem
MediumGoldman Sachs logoGoldman SachsFULLTIMEPHONE SCREEN

Problem statement

Implement a deque of strings with a doubly linked list and process operations in order.

  • push_front value and push_back value insert a value.
  • pop_front and pop_back remove and return one end, or return EMPTY.
  • front and back return one end without removal, or EMPTY.
  • size returns the decimal size.

Return one string for every pop, read, or size command. Every operation must run in O(1) time.

Function

processStringDeque(operations: String[]) → String[]

Examples

Example 1

operations = ["push_back a","push_front b","front","back","size","pop_front","pop_back","pop_back"]return = ["b","a","2","b","a","EMPTY"]

Values are read and removed from both ends; the final pop sees an empty deque.

Example 2

operations = ["front","push_back x","pop_back","size"]return = ["EMPTY","x","0"]

An empty read is reported, then x is inserted and removed.

Constraints

  • 1 <= operations.length <= 200000
  • Inserted values are nonempty strings without spaces and are never the reserved word EMPTY.
  • Every operation uses one of the documented forms.

More Goldman Sachs problems

drafts saved locally
public String[] processStringDeque(String[] operations) {
  // write your code here
}
operations["push_back a","push_front b","front","back","size","pop_front","pop_back","pop_back"]
expected["b", "a", "2", "b", "a", "EMPTY"]
checking account