Ebay Interview Question
SDE1sCountry: United States
Interview Type: In-Person
struct data
{
string user_id;
string operation;
int time;
}
struct seq_count
{
string operation[2];
int count;
};
map<string, int > frequencyMap;
map<string, seq_count> sequenceMap;
map<string, seq_count>::iterator seqItr;
while ( //We have data )
{
string key;
seq_count seq_data;
data myData = //Incoming Data;
seqItr = sequenceMap.find(myData.user_id)
if( seqItr != sequenceMap.end() )
{
if( seqItr->second.count == 2 )
{
key = seqItr->second.operation[0] + seqItr->second.operation[1] + myData.operation;
frequencyMap[key]++;
sequenceMap.earse( seqItr );
}
else
{
seqItr->second.operation[seqItr->second.count++] = myData.operation;
}
}
else
{
seq_data.count = 0;
seq_data.operation[seq_data.count++] = myData.operation;
sequenceMap[myData.user_id] = seq_data;
}
}
map<string, int> ::iterator freqitr;
string ourComb;
int maxCount = 0;
for( freqItr = frequencyMap.begin(); freqItr != frequencyMap.end(); freqItr++)
{
if( freqItr->second > maxCount )
{
maxCount = freqItr->second;
ourComb = freqItr->first;
}
}
cout << "Most frequent Combination is: " << ourComb << endl;
struct data
{
string user_id;
string operation;
int time;
}
struct seq_count
{
string operation[2];
int count;
};
map<string, int > frequencyMap;
map<string, seq_count> sequenceMap;
map<string, seq_count>::iterator seqItr;
while ( //We have data )
{
string key;
seq_count seq_data;
data myData = //Incoming Data;
seqItr = sequenceMap.find(myData.user_id)
if( seqItr != sequenceMap.end() )
{
if( seqItr->second.count == 2 )
{
key = seqItr->second.operation[0] + seqItr->second.operation[1] + myData.operation;
frequencyMap[key]++;
sequenceMap.earse( seqItr );
}
else
{
seqItr->second.operation[seqItr->second.count++] = myData.operation;
}
}
else
{
seq_data.count = 0;
seq_data.operation[seq_data.count++] = myData.operation;
sequenceMap[myData.user_id] = seq_data;
}
}
map<string, int> ::iterator freqitr;
string ourComb;
int maxCount = 0;
for( freqItr = frequencyMap.begin(); freqItr != frequencyMap.end(); freqItr++)
{
if( freqItr->second > maxCount )
{
maxCount = freqItr->second;
ourComb = freqItr->first;
}
}
cout << "Most frequent Combination is: " << ourComb << endl;
Use a hashtable to store the key which is a string equal to the concatenation of the 3 and its value will be a counter incremented according to its occurence.Everytime you increment the counter check if the incremented value is greater than the maximum if yes set incremented value to this new value and store the string sequence .Return string sequence at the end of file.
For simplifying suppose we have an array of the types of pages
public ArrayList<String> highestSequence(String [] typeofPage)
{
int maxCount=0;
ArrayList<String> sequenceMax=new ArrayList<String>();
Hashtable<String,Integer> pageIndex=new Hashtable<String,Integer>();
for(int i=0;i<typeofPage.length();i++)
{
if(i+2 <typeofPage.lenght() && i+1 <typeofPage.lenght()) // check if array has 3 elements
{
if(pageIndex.containsKey(typeofPage[i]+typeofPage[i+1]+typeofPage[i+2])
{
int a=pageIndex.get(typeofPage[i]+typeofPage[i+1]+typeofPage[i+2]);
pageIndex.put(typeofPage[i]+typeofPage[i+1]+typeofPage[i+2],a+1);
if(a+1> maxCount)
{
maxCount=a+1;
sequenceMax.add(typeofPage[i]);
sequenceMax.add(typeofPage[i+1]);
sequenceMax.add(typeofPage[i+2]);
}
}
else
pageIndex.put(typeofPage[i]+typeofPage[i+1]+typeofPage[i+2],1);
}
}
public ArrayList<String> highestSequence(String [] typeofPage)
Time Complexity O(n);
Space Complexity O(N) n ==no of typeOfPafe Entries
{
int maxCount=0;
ArrayList<String> sequenceMax=new ArrayList<String>();
Hashtable<String,Integer> pageIndex=new Hashtable<String,Integer>();
for(int i=0;i<typeofPage.length();i++)
{
if(i+2 <typeofPage.lenght() && i+1 <typeofPage.lenght()) // check if array has 3 elements
{
if(pageIndex.containsKey(typeofPage[i]+typeofPage[i+1]+typeofPage[i+2])
{
int a=pageIndex.get(typeofPage[i]+typeofPage[i+1]+typeofPage[i+2]);
pageIndex.put(typeofPage[i]+typeofPage[i+1]+typeofPage[i+2],a+1);
if(a+1> maxCount)
{
maxCount=a+1;
sequenceMax.add(typeofPage[i]);
sequenceMax.add(typeofPage[i+1]);
sequenceMax.add(typeofPage[i+2]);
}
}
else
pageIndex.put(typeofPage[i]+typeofPage[i+1]+typeofPage[i+2],1);
}
}
returnsequenceMax
}
your answer is good but how do you explain the input array. i.e typeOfpage[]
I think in question they say they have timestamp, userid and type of page visited. So before calling in the function one needs to parse the input and form a hashmap<userid,arraylist or array> which holds the sequence of actions by particular user. it takes O(n) and then u call this for each hashmap entry
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.*;
public class HelloWorld{
//initialize HashMap
public HashMap<String, Integer> times = new HashMap<String, Integer>();
public HashMap<String, String> patterns = new HashMap<String, String>();
public int maxCount = 0;
public String maxPat = "";
public void countSeq(String uID, String cat)
{
if(patterns.contains(uID))
{
String lastThree = patterns.get(uID);
if(lastThree.length() == 3)
{
lastThree = lastThree.substring(1) + "" + cat;
patterns.put(uID, lastThree);
if(times.contains(lastThree))
{
int count = times.get(lastThree);
times.put(times, ++count);
if(max < count)
{
max = count;
maxPat = lastThree;
}
}
else{times.put(lastThree,1);}
}
else if(lastThree.length() == 2)
{
lastThree = lastThree + "" + cat;
patterns.put(uID, lastThree);
if(times.contains(lastThree))
{
int count = times.get(lastThree);
times.put(times,++count);
if(max < count)
{
max = count;
maxPat = lastThree;
}
}
else{times.put(lastThree,1);}
}
else
{
lastThree = lastThree + "" + cat;
patterns.put(uID, lastThree);
}
}
else
{
patterns.put(uID,cat);
}
}
public static void main(String[] args)
{
String[] entries = {"<user id> <type of page visited> <time stamp>"};
//iterate trough entries
for(int i = 0; i < entries.length ;i++)
{
//get the uID
//Pattern p = Pattern.compile(".");
Pattern p = Pattern.compile("<(.*)> <(.*)> <(.*)>");
Matcher m = p.matcher(entries[i]);
if (m.find()) {
//get the uID and the category
String uID = m.group(1);
String cat = m.group(2);
}
if(uID ! = null && cat ! = null) countSeq(uID, cat.charAt(0));
}
System.out.println(maxPat);
}
}
The easiest way that i can think of quickly is:
- kirankumarcelestial February 11, 2014Create a array whose length is all the combination of sequence :
Arr[Home+Search+Search]
Arr[Search+Search+Search]
Arr[Search+Search+Checkout]
and increment Arr[i] once you find a sequence.