Google Interview Question
Software EngineersCountry: United States
#include <fstream>
#include <sstream>
#include <string>
#include <unordered_set>
#include <vector>
unordered_set<string> GetUniqueLines(vector<string>& str )
{
unordered_set<string> res;
for ( string filename: str ){
std::string line;
std::ifstream infile(filename);
if ( !infile ) {
continue;
}
while (std::getline(infile, line)) {
std::istringstream iss(line);
res.insert(line);
}
}
return res;
}
def findUniqueLines(path):
files = os.listdir(path)
commonlines = open(path+"\\"+files[0],"r").readlines()
for i in range(1,len(files)):
if len(commonlines) == 0:
return "No common lines"
commonlines = list(set(commonlines)&set(open(path+"\\"+files[i],"r").readlines()))
if len(commonlines) == 0:
return "No common lines"
return commonlines
Does definition of unique mean unique across all files, or unique line within a particular file allowing duplicate lines in other file?
- practice April 13, 2017