(Reject) Implement key value store | FAANG interview | April 2021
Anonymous User
678

Did a coding interview with a Big N company for a Software Engineer position

Technical round

# Implement a simplified version of zookeeper, a key value store
#
# Zookeeper requires all of the names/keys to look like paths in a filesystem, so a name is a sequence of path elements separated by a slash ("/").
# Every mapping has a parent whose path is a prefix with one less element; the exception to this rule is root ("/") which has no parent.
# Unlike a regular file system, all these mappings can have both data (so it acts like a file) and children (so it acts like a directory)
#
#                         (tree visualization example)
#                                    "/"  
#                      /                             \
#				   "/app1"                        "/app2" ...
#     /            /         \
# "/app1/p1"  "/app1/p2" "/app1/p3" ... 
#
# create(path, value)
# Creates a new path-value mapping in the zookeeper
# path is a "/" separated pathname like "/app1/p1". value is the value to be stored in the node. 
# create should signal an error to the caller if the path already exists, or if the parent of the path does not exist yet
#
# read(path)
# Returns the value of the mapping represented by path
# read should raise an error if path doesn't already exist
#
# update(path, value)
# Update the value at an already existing mapping
# update should raise an error if path doesn't already exist
#
# After implementing and testing the first three methods, please implement the additional method 
#
# watch(path, watcher)
# Sets a watcher that will be called whenever the watched path or any of its descendants are updated by a call to update or create
# watch accepts path and some sort of listener object (depending on the language this can be a function, pointer, object, or something else)
# Whenever the value of path or that of any of its descendants are set, the callback registered for the path must be called
# The watcher should accept two arguments - the path that was changed and the value that was set

class Zookeeper():
    store = {"/":None}
    def create(self, path, value):
        if self.validate(path):
            self.store[path] = value
        else:
            pass
            #raise KeyError("create error: {} invalid".format(path))
    
    def read(self, path):
        if path in self.store.keys():
            return self.store[path]
        else:
            return "read error: {} does not exist".format(path)
    
    def update(self, path, value):
        if path in self.store.keys():
            self.store[path] = value
        else:
            return "update error: {} does not exist".format(path)

    def validate(self, path):
        if path is None:
            return False
        if len(path.strip()) == 0:
            return False
        if path[0] != "/":
            return False
        if path.rindex("/") == path.index("/"):
            return True
        if path[0:path.rindex("/")] not in self.store:
            return False
        if path in self.store:
            return False
        return True


zk = Zookeeper()
zk.create("/app1", "/app1 value")
print(zk.read("/app1"))

zk.create("/app1/p1", "/app1/p1 value")
print(zk.read("/app1/p1"))

zk.create("/p1/p1", "/p1/p1 value")
print(zk.read("/p1/p1"))

zk.create("/p1", "/p1 value")
print(zk.read("/p1"))

zk.create("/p1/p1", "/p1/p1 value")
print(zk.read("/p1/p1"))

Above is the question asked and the initial solution

The Feedback was that the coding fell short or it was not up to par. No additional information was given

Implementation has error checking for some edge cases also

Did not have time for the last method

My question is what was lacking in the coding section? What can I do to improve?

Thanks

Comments (2)