## Ebay Interview Question for SDE1s

Country: United States
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
0
of 0 vote

The easiest way that i can think of quickly is:

Create 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.

Comment hidden because of low score. Click to expand.
0

what if you'll have new types of pages?

Comment hidden because of low score. Click to expand.
0
of 0 vote

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;

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````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;``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}
}
else
pageIndex.put(typeofPage[i]+typeofPage[i+1]+typeofPage[i+2],1);

}
}``````

Comment hidden because of low score. Click to expand.
0

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;
}
}
else
pageIndex.put(typeofPage[i]+typeofPage[i+1]+typeofPage[i+2],1);

}
}
returnsequenceMax
}``````

Comment hidden because of low score. Click to expand.
0

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

Comment hidden because of low score. Click to expand.
0
of 0 vote

the purpose of timestamp ?. if the user has home search search in morning and home search checkout in the evening. do these need to tie up ? like home search search and search search home and home search checkout ?

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````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);
}
}``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.