Abhi
BAN USER
- -1of 1 vote
AnswersThis is a interview question which needs to be optimized for time.
- Abhi in India
Suppose you have a 2 dimensional Array and you have a String say "Amazon" inside the Array such that the individual characters can be present from Left to Right, Right to Left, Top to down and down to up.
I will explain with example :
char[][] a = {
{B,B,A,B,B,N},
{B,B,M,B,B,O},
{B,B,A,B,B,Z},
{N,O,Z,B,B,A},
{B,B,B,B,B,M},
{B,B,B,B,B,A}
};
The above Array has two Amazon Strings. You need to return the count of number of such strings present.| Report Duplicate | Flag | PURGE
Amazon SDE-2 - 0of 0 votes
AnswersHow can I find if a String exists in a double dimension Array. For eg. Does CAT exist in
- Abhi in United States
'c','b','k'
'd','a','l'
'g','t','i'
It does exist. What will be an optimal way to do it ?
Assume you don't have to move upwards in the Array.
So in the below Array cat does not exist.
'c','b','t'
'd','a','l'
'g','J','i'| Report Duplicate | Flag | PURGE
Adobe Computer Scientist Algorithm - 0of 0 votes
AnswersFind the occurences of each word in a sentence/
- Abhi in India
e.g :
"Hi I am there, am good"
am=2, good=1,Hi=1, I=1,there=1
Print in sorted order as per count.
If count is same, it should be alphabetically sorted.| Report Duplicate | Flag | PURGE
Adobe Android test engineer Algorithm
Search for K Nearest Neighbors Algorithm for the best answer. However since this was asked in an interview looks like the below approach would work. You have N Cordinates of Taxis and you have a Point P(x,y) which is your location. Using the nearest point algorithm find the nearest point to P. This gives you a cab which is closest to You. Now you delete this cab and find another cab closest to you. Do this 3 more times and you get the closest 5 cabs to you.
- Abhi July 07, 2016You need a windowing approach to solve this problem. Actually the use case has been picked straight from Apache Spark which does many such operations. For the sake of this question you need to create a Min Heap for each product id. The timestamp decides which element is the root. Whenever you do a read or insert operation you delete elements from the heap till the only elements that are left are less than a month old and then you do the insert or read operation.
- Abhi February 27, 20161. Write multi threaded code which simulates emails from multiple hosts.
2. Simulate other factors like new users joining/ adminstrative tasks/emails/spams.
3. Since he talks about distribution, test node failures.
4. Test replication of storage.
5. Test storage of attachments.
6. Test across data centres.
7. Test with timezones. Various timezones have various loads at different times.
8. Test dispatch of advertisements.
If you want to search on any of the categories like ItemNo or Authour or title etc you would need multiple maps.
1. You can create object which encapsulates everything.
2. Create multiple maps with Keys like(Title/Author/Product No) and value is a pointer to the Object.
You need not store the whole objects in the map.
Create a BST with words from the Files. Each Node in the tree will have below structure
Node{
String word;
String[] fileName;
int count
int[] line
}
For each word increment the count and insert the File name and line# in node structure
Once you are done creating the tree. Remove all Nodes that have count as 1.
You can design the system in 2 ways :
1. Implement the virtual memory yourself and use File IO operations if and when you need more memory than existing memory. You need to swap some of your data to a File to create space for new Objects.
2. You try to clean memory just like a garbage collector does and remove unwanted objects from time to time. This is inferior to 1 and you run the risk of your program crashing because of lack of memory.
You can use a combination of 1 & 2.
I have a variation of the program already written.
Run it.
Here F = 1, means dead end path.
This is recursive convert to DP.
public class printPossiblePathsInMatrix {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
char[][] arr = new char[][]{
{'A','B','F'},
{'C','D','F'},
{'E','G','H'}
};
possiblePath("", arr, 0, 0);
}
private static void possiblePath(String str, char[][] a, int r, int c)
{
if(r > 2 || c > 2)
return;
if(r==2 && c==2){
str+= a[r][c];
System.out.println("---" + str);
return;
}
if(c+1 < 3)
if(a[r][c+1] != 'F'){
String s1 = str + a[r][c];
possiblePath(s1, a, r, c+1);
}
if(r+1 < 3)
if(a[r+1][c] != 'F'){
String s2 = str + a[r][c];
possiblePath(s2, a, r+1, c);
}
if( r+1 < 3 && c+1 < 3)
if(a[r+1][c+1] != 'F'){
String s3 = str + a[r][c];
possiblePath(s3, a, r+1, c+1);
}
}
}
This is a DP problem. At each node you can think of path covered so far and the optimal solution to reach the end from here. Without recursion also gives a hint that DFS is not the answer. Look for min cost path problem in DP section at geeksforgeeks for a variation to this problem.
- Abhi February 17, 2013This is an implementation of the Push/Pull Model. The first time you go online you pull the data of your online friends, there after you push your name into the Active Maps of all the Users. When thier status changes they push their status to all the people in the Map. Read Observer/Observable design pattern for this.
- Abhi February 16, 2013public class PermutationTest {
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
String permString = "ABCDE";
Set<String> se = permutations(permString);
Iterator<String> itr = se.iterator();
System.out.println("Total size of Permutations = " + se.size());
while(itr.hasNext()){
System.out.println("--" + itr.next());
}
}
public static Set<String> permutations(String str)
{
Set<String> se = new HashSet<String>();
permute(str.toCharArray(),"",se);
return se;
}
public static void permute(char[] rem, String str, Set<String> se)
{
if(rem.length == 1){
String str2 = str + rem[0];
se.add(str2);
return;
}
for(int i = 0 ; i < rem.length ; i++)
{
char[] rem2 = getRemainingArray(rem,i);
String s = "" + str + rem[i];
permute(rem2, s, se);
}
}
private static char[] getRemainingArray(char[] rem, int i)
{
char[] rem2 = new char[rem.length - 1];
int k = 0;
for(int j = 0 ; j < rem.length ; j++)
{
if(i!=j){
rem2[k] = rem[j];k++;
}
}
return rem2;
}
}
Very strange question. What's the big deal. There has to be atleast one word and the sentence should start from it. Start a linear traversal from beginning and keep on adding characters, for every character added see if the word formed so far is a well formed word. If NO add more characters. If Yes, print this valid word and repeat the process for the remaining sentence.
- Abhi February 02, 2013
It is easy to think it is a largest number problem but it is not. For eg : List(9,6,3,0). largest number is 9630 which is divisible by 3. Like wise 3069 is divisible by 3. It is about taking a set of numbers and creating a subset which is divisible by 3. It is a subset sum problem and the subset sum should be divisible by 3.
- Abhi February 25, 2017