MrA
BAN USER
- -2of 4 votes
Answerspublic class MyClass {
- MrA in United States
public static int num=1;
public static boolean flag=false;
public static void main(String[] args) {
Thread t =new Thread(new MyThread());
t.start();
MyClass.flag=true;
MyClass.num=10;
}
}
class MyThread implements Runnable{
public void run() {
while(!MyClass.flag){
Thread.yield();
}
System.out.println(MyClass.num);
}
}
Output of this code and the reason for the output?| Report Duplicate | Flag | PURGE
Walmart Labs Software Engineer Java - 1of 1 vote
AnswersFind Common Ancestor of given two nodes A and B
- MrA in United States
Condition :Node does not have the parent pointer and data value.
So its is like class Node {
Node L;
Node R;
}
I mentioned below Solution. Since they didnt give me Parent I created one for each node than did back tracking from two given Nodes A & B;
Node commonAncestor(Node A, Node B, Node root){
HashMap<Node> map=new HashMap<Node>();
Queue q=new Queue();
map.(root,null);
q.enqueue(root);
while(!q.isEmpty()){
Node curr=q.dequeue();
if(curr.L !=null)
map.put(curr.L,cur);
if(curr.R !=null)
map.put(curr.R,cur);
}
Node prev=A;
Node next=null;
while(map.contains(prev)){
next= map.get(prev);
map.remove(prev);
prev=next;
}
prev=B;
next=null;
while(map.contains(prev)){
prev=map.get(prev);
}
}| Report Duplicate | Flag | PURGE
Goldman Sachs - 0of 0 votes
AnswersWhen you were in a conflict with a teammate or team member and he persuaded you to go his way? What would you do?
- MrA in United States| Report Duplicate | Flag | PURGE
Software Analyst Behavioral - 0of 0 votes
Answerswhat will you do when client is asking you to provide something which doesnt make any sense?
- MrA in United States| Report Duplicate | Flag | PURGE
Software Analyst Behavioral - -3of 3 votes
AnswersRequirements:
- MrA in United States
1>client should be able to create shapes like circle, square , rectangle..etc,. Also framework should be able to add some more shapes later.
2> client should be able to calculate areas of each shapes
3> should have collection where all shapes are stored and should be able to sort on the area of the shapes| Report Duplicate | Flag | PURGE
Amazon SDE1 Object Oriented Design - 0of 0 votes
AnswersFastest String sorting algorithm?
- MrA in United States
Suppose you have file which has many strings. How will you sort all strings in that file. That file size is large.
Time complexity ?
Space complexity?| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm - 0of 0 votes
AnswersGiven function List friendList(Person p) which will give all freinds list of that person. Write a function such that printConnectedLeastFriends(Person A, Person B); if none of the friends found connected print "NONE";
- MrA in United States| Report Duplicate | Flag | PURGE
Amazon SDE1 Algorithm
Thats nice solution, But there are many case where this approach can break your solution.
1> if 1st element is 0 than the whole multiplication will be zero. Also this apply to the last element.
2> Your multiplication can overflow if large input is provided. Large in terms of array size or element it self: In this case you have to use BigInteger in java
I would use Max heap data structure key for this data structure would be Balance of the Person
So when max balance is query is requested you will get at O(1). But when the persons balances changes based on the heap DS it will chnage its position in the tree which will be O(lgn)..
So building heap will be O(n)..I would represent class:
class Person{
String name;
String location;
float balance;
int day;
float credit(float amount);
boolean debit(float amount);
int getDay();
String getName();
String getLocation();
}
HashSet<String> permutaion(String s){
HashSet<String> set=new HashSet<String>();
permutation(" ", s,set);
}
void permutation(String prefix,String s,HashSet<String> set){
if(s.length()==0){
set.add(prefix);
System.out.println( prefix);
}
for(int i=0;i<s.length();i++){
permutation(prefix+s.charAt(i),s.substring(0, i)+s.substring(i+1, s.length()),set);
}
}
Unique words will be = n!/ (number of Repeated Characters);
- MrA July 14, 2013Your solution works for the common friends list not the connected friend list. This is the problem of graph. Using shortest path Algorithm can be solved this problem.
For eg: Suppose Alphabets represent person.
|A| - F-G-H-U-I
|U| - W-G-H-N-J-K-A
|k| - I-A-J-P-B-U
|P| - Q-G-J-K-B
|B| - K-O-L-M-Y
Answer should be U,K
Since A-U-K-B
All operations on Hashtable or Hashmap are O(1) assuming the uniform distribution of the key in the table and avoiding the collision of the key. Avoiding collision and uniform distribution is all depends how hash function is well implemented which is very critical implementation. But the aim of this data structure is providing operations at O(1).
So if the hash function is really bad than the worst case will be O(N).
static void printUniqueList(int list1[],int list2[]){
int i=0,j=0;
while(i<list1.length || j<list2.length )
{
if(i<list1.length){
if(list1[i]<list2[j]){
System.out.println( list1[i++]);
}else if(list1[i]!=list2[j]){
System.out.println( list2[j++]);
}else{
i++;
j++;
}
}else{
System.out.println( list2[j++]);
}
}
}
}
}
Time= O(N)
Space= O(1)
int findFirstOne(int array[]){
int length=array.length;
int head=0,tail=length-1;
while(head<tail){
int middle=head+(tail-head)/2;
if(array[middle]==0)
head=middle+1;
else
tail=middle;
}
if(head==tail && array[head]==1)
return head;
return -1;
}
}
int a1[]={0,0,0,0,0,0,0,0,0,0,0};
int a2[]={0,0,1,1,1,1,1,1};
int a3[]={1,1,1,1,1,1,1,1,1};
int a4[]={0,0,0,0,0,0,0,0,0,0,1};
System.out.println(" a1 ="+findFirstOne(a1)+" a2 ="+findFirstOne(a2)+" a3="+findFirstOne(a3)+" a4= "+findFirstOne(a4));
OUTPUT------------------------------------
a1 = -1 a2 =2 a3 =0 a4= 10
time complexity = O(lgn)
changed the question..
- MrA March 30, 2015