Created
May 24, 2018 20:24
-
-
Save jianminchen/82875a3296a63fcb6be750ae5ef53cac to your computer and use it in GitHub Desktop.
Being an interviewer - suggest char maximum occurrence given prefix - Julia learned to be an interviewer and compare the interviewee to herself.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Suppose you are supplied with a file containing a list of words like ABC,ACD, ABC | |
( say each word in new line ). now you have to suggest algorithm for this problem - | |
When a user type some character, we have to suggest him next character and basis of | |
suggestion is that the character you are going to suggest should have maximum occurrence | |
at that position with the given prefix. | |
For example , Let's say words are | |
ABC | |
ACD | |
ABC | |
Now if user types 'A' we have to suggest him 'B' as next character with the prefix ‘A’, ‘B’ | |
has 2 times, and ‘C’ has 1. similarly if he types 'AB' then we need to suggest him third | |
character as 'C' as in third index since C has 2 times | |
Julia asked the interviewee to work on trie and put some numbers together before the interviewee wrote the code: | |
ABC | |
ACD | |
ABC | |
A - Z -> | |
A 3 | |
/ \ | |
B 2 C 1 | |
/ \ | |
C 2 D 1 | |
Advice Julia shared: | |
pramp.com - sharp your skills - structure your interview | |
go through requirement -> missing anything? constraint | |
-> good communication | |
-> udemy.com jeff bae - 14 dollars -> how much | |
pramp.com - free -> | |
*/ | |
using System; | |
class Solution | |
{ | |
static void Main(string[] args) | |
{ | |
var sol=new Solution(); | |
var inputWords=new List<string>{"ABC","ACD","ABC"}; | |
var output=sol. | |
} | |
public char FindNextSuggestion(List<string> words, string str){ | |
var trieRoot=new TrieNode(); | |
int index=0; | |
foreach(var word in words){ | |
trieRoot.Add(word,index++); | |
} | |
var runner=trieRoot; | |
foreach(char c in str){ | |
if(runner.Children[c-'A']==null) | |
return ''; | |
runner=runner.Children[c-'A']; | |
} | |
TrieNode maxTrieNode=null | |
for(var i=0;i<26;i++){ | |
if(runner.Children[i]==null) | |
continue; | |
if(maxTrieNode==null){ | |
maxTrieNode=runner.Children[i]; // orderIndex | |
continue; | |
} | |
if(maxTrieNode.Count<runner.Children[i]){ | |
maxTrieNode=runner.Children[i]; | |
} | |
else if(maxTrieNode.Count==runner.Children[i] && maxTrieNode.OrderIndex>runner.Children[i].OrderIndex){ | |
maxTrieNode=runner.Children[i]; | |
} | |
} | |
return maxTrieNode.Value; | |
} | |
} | |
class TrieNode{ | |
public char Value{get;set;} | |
public int Count{get;set;}' | |
public int OrderIndex{get;set;} | |
public TrieNode[] Children{get;set;} | |
public TrieNode(char val,int index){ | |
this.Value=val; | |
Children=new TrieNode[26]; | |
this.Count=1; | |
this.OrderIndex=index; | |
} | |
public TrieNode(){ | |
} | |
public void Add(string word,int index){ | |
var runner=root; | |
foreach(char c in word){ | |
if(runner.Children[c-'A']==null){ | |
runner.Children[c-'A']=new TrieNode(c,index); // | |
//runner.chi....OrderIndex = index; | |
} | |
else{ | |
runner.Children[c-'A'].Count++; | |
} | |
runner=runner.Children[c-'A']; //? | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment