ash.taunk3
BAN USER- 0of 0 votes
AnswersIf I am designing a media player and I want to store songs and play them in random order
- ash.taunk3 in India
a) what data structure will you use to store songs?
b) how will select the next song to play in a way which prevents the same song being played in consecutive turn| Report Duplicate | Flag | PURGE
Amazon Data Structures - 0of 2 votes
AnswersGiven N ropes of lengths L1, L2, L3, L4, …, LN. I had to join every rope to get a final rope of length L1 + L2 + … + LN.
- ash.taunk3 in India
However, I can join only two ropes at a time and the cost of joining the two ropes is L1 + L2. I was supposed to join ropes in such a way that the cost is minimum.| Report Duplicate | Flag | PURGE
Amazon SDE1 Algorithm
@praveen.
Yes it is easy to search a particular location(x,y) in K-d tree.(say k=2)
But could you please explain how to find nearest 10 locations from a particular location (x,y) ?
you are miss-interpreting the question.
1st : you have to join all the ropes available
2nd: total length should be sum of all ropes.
This is wrong statement
if(root == null)
return (root.data == sum);
What if we sort on basis of start time
Would it work ?
What is the need of Modifying the matrix ?
matrix[currx][curry] = 1;
Overriding CompareTo() is good approach.
Most preferable algo for this will be use MIN HEAP of size i.
Heap<Item*> h;
Algo:
1. Add first i objects to Heap
2. for each remaining element in list
3. if(next obj > h.getMIN()) //CompareTo()
{
Remove MIN, Add next obj. to Heap;
Heapify(); // O(log i)
}
3. H.GetMIN(). is the Most popular item.
1. Set some B's variable in A's constructor. For this we need to be sure that instance of B is already created before creating instance of A.
This is kind of sharing a variable within classes. More generic implementation will be like Observer pattern.
class B
{
public:
bool isACreated;
B()
{
isACreated = false;
}
bool FnIsACreated(){ return isACreated;}
};
class A
{
A(const B & objB)
{
objB.isACreated = true;
}
};
This is very simple and basic implementation.
- ash.taunk3 June 02, 2015But topological ordering of Cyclic graph will not be correct. How can we handle if there is cycle
- ash.taunk3 May 24, 2015vptr
gets created in both kind of objects (statically created or dynamically created)
class base{
int i;
virtual void show() {..}
};
1. Statically created:
base b;
2. Dynamically created:
base *b = new base();
Point is when you access any virtual function through these objects then vptr is used or not.
case 1st :- Vptr is not used. So even if vptr is NULL, still virtual function can be called, because there is no polymorphism.
case 2nd:- Vptr is used. If Vptr is NULL , then calling to virtual function will give null pointer exception.
we can use command pattern here.
we create separate class of each button. All those are derived from single base class having virtual execute() function.
since there is polymorphism on execution of button press, so no need of conditional statements.
Also in command pattern we can queue the button press function.
How will we avoid to add duplicate entries in Heap
- ash.taunk3 October 09, 2014vptr gets created in both kind of objects (statically created or dynamically created)
class base{
int i;
virtual void show() {..}
};
1. Statically created:
base b;
2. Dynamically created:
base *b = new base();
Point is when you access any virtual function through these objects then vptr is used or not.
For case 1st - Vptr is not used. So even if vptr is NULL, still virtual function can be called, because there is no polymorphism.
For case 2nd Vptr is used. If Vptr is NULL , then calling to virtual function will give null pointer exception.
It will not crash if object created statically.
- ash.taunk3 September 18, 2014
If you had already done this, then now do you u want some other solution ?
- ash.taunk3 March 06, 2016