0% found this document useful (0 votes)
387 views4 pages

Trifacta Interview Questions: Round 1

It presents solutions in C++ to three coding problems - finding the longest distinct substring by maintaining a map of character frequencies, implementing an LFU cache that tracks frequencies using maps and lists, and designing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
387 views4 pages

Trifacta Interview Questions: Round 1

It presents solutions in C++ to three coding problems - finding the longest distinct substring by maintaining a map of character frequencies, implementing an LFU cache that tracks frequencies using maps and lists, and designing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Trifacta Interview Questions

Round 1:
Longest substring of distinct characters. (Leetcode Medium)
Sol.
#include<bits/stdc++.h>
using namespace std;
 
string lsdc(string str) {
     int j = 0, maxLen = 0;
     string res = "";
     unordered_map<char, int> charCount;
     for (int i=0; i<str.size(); i++) {
        while(charCount[str[i]] == 1) {
          charCount[str[j]]--;
            if(charCount[str[j]] == 0)charCount.erase(str[j]);
            j++;
        }
        charCount[str[i]]++;
        if (i-j+1 > maxLen) {
           maxLen = i-j+1;
           res = str.substr(j, maxLen);
        }
    }
    return res;
}
 
int main () {
    vector<string> testcases = {"geeksforgeeks", "abcabcb", "pwwkw"};
    for (string str : testcases) {
         string res = lsdc(str);
         cout<<res<<" ("<<res.size()<<")"<<endl;
    }
    return0;
}

Implement Least Frequently Used (LFU) Cache. (Leetcode Hard)


Sol.
#include<bits/stdc++.h>
using namespace std;
 
class LFUCache {
    int cap, size, minFreq;
    unordered_map<int, pair<int, int>> m; // key to {value,freq}
    unordered_map<int, list<int>::iterator> mIter; // key to list-iter
    unordered_map<int, list<int>> fm;  // freq to key list
public:
    LFUCache(int capacity) {
        cap = capacity;
        size = 0;
    }
   
    int get (int key) {
        if(m.count(key) == 0) return-1;
       
        fm[m[key].second]. erase(mIter[key]);
        m[key]. second++;
        fm[m[key]. second].push_back(key);
        mIter[key] = --fm[m[key]. second].end();
       
        if(fm[minFreq].size() == 0) minFreq++;
       
        return m[key]. first;
    }
   
   void put (int key, int value) {
        if (cap <= 0) return;
       
        int storedValue = get(key);
        if(storedValue != -1) {
            m[key]. first = value;
            return;
        }
       
        if (size >= cap) {
            m.erase(fm[minFreq].front());
            mIter.erase(fm[minFreq].front());
            fm[minFreq].pop_front();
            size--;
        }
       
        m[key] = {value, 1};
        fm[1].push_back(key);
        mIter[key] = --fm[1]. end();
        minFreq = 1;
        size++;
    }
};
 
int main () {
LFUCachelfu(2);
lfu.put(1, 1);
lfu.put(2, 2);
cout << lfu.get(1) << endl;    
lfu.put(3, 3);   
cout << lfu.get(2) << endl;     
cout << lfu.get(3) << endl;      
lfu.put(4, 4);   
cout << lfu.get(1) << endl;
cout << lfu.get(3) << endl;
cout << lfu.get(4) << endl;

return 0;
}

Round 2:
Implement Least Recently Used (LRU) Cache. (Leetcode Hard)
Sol.
#include <bits/stdc++.h>
using namespace std;
 
class LRU {
  int size;
  list<pair<int, int>> dl;
  unordered_map<int, list<pair<int, int>>::iterator> mp;
 
public:
  LRU (int capacity) {
    size = capacity;
  }
 
  int get (int key) {
    if(mp.find(key) == mp.end()) {
      return -1;
    } else {
      auto it = mp[key];
      int val = it->second;
        dl.erase(it);
       dl.push_front({key, val});
       mp[key] = dl.begin();
       return val;
    }
  }
 
  void add (int key, int val) {
    if(mp.find(key) == mp.end()) {
      if(mp.size() == size) {
        auto rit = dl.rbegin();
        mp.erase(rit->first);
        dl.pop_back();
      }
    } else {
      auto it = mp[key];
      dl.erase(it);
    }
    dl.push_front({key, val});
    mp[key] = dl.begin();
  }
 
  void displayCache() {
     for (auto it=dl.begin(); it!=dl.end(); it++) {
      cout<<it->first<<" "<<it->second<<endl;
    }
  }
};
 
int main () {
  LRU lru(5);
  lru.add(1, 10);
  lru.add(2, 10);
  lru.add(3, 10);
  lru.displayCache();
  cout<<lru.get(1);
  lru.add(4, 10);
  lru.add(5, 10);
  lru.add(6, 10);
  lru.displayCache();
return 0;
}

You might also like