You are given an array of strings:
["ad", "awe", "cat", "apple", "dog", ...]You are also provided with an API:
bool isValid(string s)This API internally contains a constant string P and returns true if P is a prefix of s. Example:
P = "a"
isValid("ad") -> true
isValid("awe") -> true
isValid("apple")-> true
isValid("cat") -> false
isValid("dog") -> falseYour task is to rearrange the array so that all valid strings appear at the beginning, while minimizing the number of movements in-memory where the array is stored. The relative order of valid strings does not necessarily need to be preserved.
Also, tell the time and space complexity accounting for the API call as well.
Standard Googlyness round. Questions around conflicts, my work experience, situations and my approach to dealing with them.
Design a class that generates unique strings to be used for URL shortening.
Each generated string must follow the constraints below:
a-z.T is provided.T.This means:
You need to implement a class similar to:
class URLShortener {
public:
URLShortener(int threshold);
string getNext(); // Returns the next unique valid string
};URLShortener(int threshold) initializes the generator.getNext() returns the next unique string satisfying the constraints.Possible sequence of outputs:
Length 1
a
b
c
...
z
Length 2
aa
ab
ac
...
zzAll characters in each string appear at most 2 times.
Implement LRU Cache with Expiration
Part 1: Standard LRU cache question. Design and implement an LRU (Least Recently Used) Cache that supports the following operations:
get(key) → Returns the value associated with the key if it exists in the cache, otherwise return -1.put(key, value) → Insert or update the value of the key.class LRUCache {
public:
LRUCache(int capacity);
int get(int key);
void put(int key, int value);
};Part 2: Extend the cache to support time-based expiration.
Each entry should expire after a fixed time window, for example 5 minutes.
class LRUCache {
public:
LRUCache(int capacity, int expirationTime);
int get(int key);
void put(int key, int value);
};The questions aren't very tricky so I'll drop just hints/statements of what I did during the interviews.
Phone Interview 1 - 2 pointer approach - I thought about first how is the movement minimized. And then was going with maintaining an extra array to track valid strings but then interviewer hinted and I caught the 2 pointer approach. Important to tell time complexity accounting for the API call, how is the prefix match done, O(L) - string length in API class. The API class wasn't implemented but I needed to just think it with the best design possible.
Passed to onsite interviews.
Onsite Interview 1 - I went with enumerating the strings like digits with base 26. So essentially digits 0..25 map to char a..z and then simple addition with carry over logic. Important to note that in this approach you need to make sure that an output is valid, otherwise generate next string because enumeration ensures exhaustiveness, uniqueness and 'shorter strings first' property but doesn't guarantee that a string will be valid. For. ex. with Threshold = 2, digits 110 and 112 are valid but 111 is invalid.
Told the time and space complexity.
Onsite Interview 2 - Simple LRU implementation with unordered_map and LinkedList. For expiration I lazily deleted keys when a get or put is done for an entry. The linked list maintains order of addition of keys, so if a node has expired all the previous nodes can also be cleaned up in the list.
Discussed further what if we go with a recurring job approach to clean the cache, advantages and disadvantes, time complexities in each case.