Meta || Research Scientist Infra || E4 Phone Screen
Anonymous User
1241

Completed phone screen recently, waiting to hear back (most employees are on holiday break so turnarounds seem slower). Locations on the job posting are NYC, Seattle, Menlo Park. Phone screens are supposed to be identical between SWE and RS positions at the E4-5 level, maybe also E6.

Edit: recruiter just reached out, advancing to the next round!

Got two questions, both LC Mediums:

  • 921: Minimum Add to Make Parentheses Valid -- Clarified that that string was guaranteed to be null-terminated, empty string counts as valid.
  • 560: Subarray Sum Equals k -- Easier version, interviewer just requested you return true if there exists a subarray that sums to k, and return false otherwise (i.e., you don't have to say how many subarrays exists, just if there is one). Otherwise unchanged, array can include negatives. Clarified that an empty array does not have a sum, so [] returns false for all k (including k=0), and that I was allowed to modify the input array.

Used C, got optimal solutions to both , forgot that you could use a sorting based approach for 560 because nerves. Interviewer said using uthash.h was fine. No followup questions on either.

  • 921: "stack" approach using a counter, O(n) time and O(1) space.
  • 560: hashmap approach to the original problem but broke early if any subarray was found, O(n) time and O(n) space. You could sort and then use a two pointer / sliding window approach (or binary search) for O(n log n) time and O(log n) space, I just didn't think of it in the moment. Edit: you cannot do this because the subarray has to be contiguous and cumulative sums aren't guaranteed to be strictly increasing/decreasing due to presence of positives and negatives. Thanks to the commenters for making me feel better about "missing" this, I guess I was thinking better during the screen than after lol.

Interview was through coderpad, running any code was disabled (standard for all meta phone screens). Interviewer provided some test cases and I was expected to come up with some additional ones. Walked through test cases by hand.

If people are interested in code, iirc my code was something like this (it's been a little while, and I haven't tested this code so it may not run). 560 can be done in a single pass, I just didn't want to overcomplicate things.

921: Balance parentheses
int minInsertions(char *s){
  int num_opening = 0, num_to_add = 0;
  while(*s){
    if(*s == '(') {
      num_opening++;
    } else {
      if(num_opening) num_opening--;
      else num_to_add++;
    }
    s++;
  }
  return num_to_add + num_opening;
}
560: Subarray sum equals k
typedef struct hash {
  int key;
  UT_hash_handle hh;
} hash;
hash *hashtable;

bool hasSubarraySum(int *arr, int len, int k){
  if(!len) return false;
  int cumul_sum = 0;
  for(int i=0; i<len; i++){
    cumul_sum += arr[i]
    if(arr[i] == k || cumul_sum == k) return true;
    arr[i] = cumul_sum;
  }
  
  hashtable = NULL;
  hash *tmp;
  bool retval = false;
  int diff;
  // could definitely fold this loop into the previous loop
  // I was just running out of time and wanted to be extra careful
  for(int i=0; i<len; i++){
     diff = k - arr[i];
     HASH_FIND_INT(hashtable, &diff, tmp);
     if(tmp){
        retval = true;
        break;
     }
     diff = arr[i];
     HASH_FIND_INT(hashtable, &diff, tmp);
     if(!tmp){
       tmp = malloc(sizeof(hash));
       tmp->key = diff;
       HASH_ADD_INT(hashtable, key, tmp);
    }
  }
  HASH_CLEAR(hh, hashtable);
  return retval;
}
Comments (5)