FastPrepArray-Backed Set Without Library Collections

Array-Backed Set Without Library Collections

Ode with Anthropic logoOde with Anthropic● EasyFULLTIMEPHONE SCREEN

Problem statement

Build a set of strings without a built-in set, map or other library collection doing the work for you. Keep the elements in a fixed-size array that you create once and manage yourself, and compare elements by their contents (in Java, with equals, not ==). This rule is part of the exercise; the tests can only check the returned results.

You are given two arrays of the same length, operations and values. Start with an empty set and apply the operations in order. Operation i uses the value values[i] and adds one string to the returned array:

  • "isEmpty": "true" if the set has no elements, otherwise "false".
  • "size": the number of elements in the set, as a decimal string such as "3".
  • "add": if values[i] is not in the set, add it and return "true". If it is already there, leave the set unchanged and return "false".
  • "contains": "true" if values[i] is in the set, otherwise "false".
  • "remove": if values[i] is in the set, remove it and return "true". Otherwise leave the set unchanged and return "false".

For "isEmpty" and "size", values[i] is the empty string and is not used. The set has no capacity limit other than the number of operations.

Function

runArraySet(operations: String[], values: String[]) → String[]

Examples

Example 1

operations = ["isEmpty","add","isEmpty","size","add","size","contains","contains"]values = ["","red","","","blue","","red","green"]return = ["true","true","false","1","true","2","true","false"]

The new set is empty. Adding "red" succeeds, so the set is no longer empty and has size 1. After adding "blue" the size is 2. "red" is in the set and "green" was never added.

Example 2

operations = ["add","add","size","remove","remove","isEmpty"]values = ["x","x","","x","x",""]return = ["true","false","1","true","false","true"]

The second add of "x" finds it already there, returns "false" and changes nothing, so the size is 1. The first remove deletes it; the second finds nothing to remove. The set is empty again.

Example 3

operations = ["add","add","add","remove","contains","size","add","contains"]values = ["a","b","c","a","c","","a","a"]return = ["true","true","true","true","true","2","true","true"]

Removing "a", the first element added, must not hide the others: "c" is still found and the size is 2. After the removal "a" can be added again.

Constraints

  • 1 <= operations.length <= 2000
  • values.length == operations.length
  • Each operations[i] is one of "isEmpty", "size", "add", "contains" and "remove".
  • For "add", "contains" and "remove", values[i] has 1 to 8 lowercase English letters. For "isEmpty" and "size", values[i] is the empty string.
See Ode with Anthropic hiring insights
Saved
public String[] runArraySet(String[] operations, String[] values) {
    // Write your code here.
}
Checking account…