rawat011
BAN USER- 0of 0 votes
AnswersThere is an server which has infinite number of services s1, s2, s3,....Also there is a priority attached to it. There is one to one mapping between the services and priorities. The following mapping is stored by the server.
- rawat011 in India
p1 --> S1
p2 --> s2
p3 --> s3
p4 --> s4
p5 --> s5
p6 --> s6
.
.
.
infinite
Whenever a user logs in to the system, it assigns a service with lowest priority available to it.
Whenever a user logs out of the system, service is returned to the pool of free services.
a. What is the datastructure that you will use to store these services. You dont have to store the mapping. Mapping is already stored in the system.
b. Can you write the code for it.
c. How will you go about writing automation framework to test it
Please let me know if anyone need any more info about the question| Report Duplicate | Flag | PURGE
Amazon Software Engineer in Test Algorithm
Here is my code in perl
package Song;
use Moose;
has 'playedCount' => (is => 'rw', default => 0);
has 'title' => (is => 'ro', required => 1);
sub play {
my ($self) = @_;
$self->playedCount($self->playedCount + 1);
}
1;
package Singer;
use Song;
use Moose;
has 'name' => ( is => 'ro', required => 1 );
has 'topSongObj' => (is => 'rw', predicate => '_isSet_topSongObj');
has 'songs' => (is => 'rw', isa => 'ArrayRef', default => sub { [] },);
sub play {
my ($self, $song) = @_;
my $found = 0;
foreach my $songObj ( @{$self->songs} ){
if ($songObj->title eq $song) {
$found = 1;
$songObj->play();
if ($songObj->playedCount > $self->topSongObj->playedCount) {
$self->topSongObj($songObj);
}
}
}
if (! $found) {
my $s = Song->new(title => $song);
$s->play();
if (! $self->_isSet_topSongObj) {
$self->topSongObj($s);
}
push(@{$self->songs}, $s);
}
}
sub topSong {
my ($self) = @_;
return $self->topSongObj->title;
}
1;
use Singer;
my %singers;
sub play {
my ($singer, $song) = @_;
if (! exists $singers{$singer}) {
my $s = Singer->new(name => $singer);
$singers{$singer} = $s;
$s->play($song);
} else {
my $s = $singers{$singer};
$s->play($song);
}
}
sub topSong {
my ($singer) = @_;
if (! exists $singers{$singer}) {
die "Singer hasnt sing any song yet";
}
return $singers{$singer}->topSong;
}
play('singerA' , 'song1');
play('singerA' , 'song2');
play('singerA' , 'song3');
play('singerA' , 'song1');
play('singerA' , 'song2');
play('singerA' , 'song2');
print topSong('singerA') . "\n";
c++ code
#include<iostream>
#include<string>
using namespace std;
void ReverseLetters(std::string& s)
{
int i = 0;
int start = 0;
int l = s.length();
while (i < l)
{
if (s[i+1] == ' ')
{
int end = i;
while(start < end){
char c = s[start];
s[start++] = s[end];
s[end--] = c; }
i++;
while(s[i] == ' ' and i < l)
{
i++;
}
start = i;
}
i++;
}
}
Here is the perl code
##################################################
# name: FindIntersection
# Objective: Find intersection of elements two lists, create
# new list with intersection elements and return it
##################################################
sub FindIntersection {
my ($h1, $h2) = @_;
my $h3;
while( defined $h1 && defined $h2 ) {
if ($h1->{data} < $h2->{data}) {
$h1 = $h1->{_next};
} elsif ($h1->{data} > $h2->{data}) {
$h2 = $h2->{_next};
} else {# found intersection element
_add($h3, $h1->{data});
$h1 = $h1->{_next};
$h2 = $h2->{_next};
}
}
return $h3;
}
the code is in perl but works fine
sub isBST {
my ($tree) = @_;
return 1 unless($tree);
if ($tree->{LEFT}) {
if ($tree->{LEFT}->{VALUE} > $tree->{VALUE}) {
return 0;
}
}
if ($tree->{RIGTH}) {
if ($tree->{RIGTH}->{VALUE} < $tree->{VALUE}) {
return 0;
}
}
return isBST($tree->{LEFT}) && isBST($tree->{RIGHT});
}
This can be done using merge. foreach row i in Arr Merge row(i) to row(i+1). While merging, make sure to drop the number if its duplicate.
Merging will take O(n) because rows are sorted. Merging m rows will take O(m).
Total complexity time O(n*m). Space complexity O(m+n).
perl code. Should work as it is. Just need to install List::Util cpan module if not already installed
- rawat011 July 19, 2017