D
BAN USER
- 2of 2 votes
AnswersConsider a city (visualize a circle). It has n petrol stations in it. You are given the maximum amount of petrol that can be filled at each of these stations. You are also given the distance between one station to the next one. The aim is to cover the entire city and come back to the start point. Assume that 1 liter of petrol will last for 1km.
- D in India
Q: List out all the possible petrol stations from where the journey can be started, so as to cover the city.| Report Duplicate | Flag | PURGE
Amazon Software Engineer in Test Algorithm
sub search {
my $hash_ref = shift;
my $srch_key = shift;
foreach my $key ( keys(%{$hash_ref}) ) {
if( $key eq $srch_key ) {
print "\nFound! " . $hash_ref->{$key};
return $hash_ref->{$key};
} else {
print "\nref $key == ". ref($hash_ref->{$key});
if( ref($hash_ref->{$key}) =~ /HASH/ ) {
return search($hash_ref->{$key},$srch_key);
} elsif( ref($hash_ref->{$key}) =~ /ARRAY/ ) {
my @array = @{$hash_ref->{$key}};
foreach my $element ( @array) {
if( ref($element) =~/HASH/ ) {
my $val = search($element, $srch_key);
return $val unless $val == '';
}
}
}
}
}
} # end sub search
No actually - IMHO. When the requirement is not given, they would like to see your creativity and the spread of your thought over testing. Not having proper requirements would mean you have a lot of options to test. Getting specific and specific requirements would mean cutting out a lot of stuff and restricting your thoughts.
- D October 17, 2013Functional tests for each component:
(a) Camera:
1. Quality of the picture
2. Quality of the picture when objects are in motion
3. Coverage of the picture. Upto what distance does the camera cover/not (range)
4. Quality at different time intervals (morning, evening- diff lightings)
5. Picture quality at different weather conditions - foggy, rainy season
6. Interval of taking 2 pictures
7. Power requirements to keep the camera running
(b) Embedded system
1. Speed of transmission
2. What if server is down, are pictures retransmitted? Upper limit for retrial
3. What if camera stops working? Does it keep resending the last picture taken?
4. Are the pictures data & timestamped while sending? (maybe this is for the OCR)
5. Power req to keep this running
6. Queue limit of pictures to be sent to the server (in case the server is down abruptly)
(c) OCR server
1. Saving format of the picture
2. Are the pictures compressed?
3. Date and time stamped?
4. Retrival of a picture from the database - given a date & time range
Interface tests:
1. Format of the image sent from camera to embedded system is defined, so if camera is changed later and same format is used, the system still works
2. Similarly between embedded system and OCR
This is my code. I have a feeling I'm missing something here, coz this is pure programming logic. Nothing specific to Perl.
use strict;
my $LIMIT = 9;
my $file_name = "abc.txt";
open(FILE,"<$file_name");
my @lines = <FILE>;
my @new_lines = ();
my $new_ind = 0;
foreach my $line (@lines) {
if( length($line) > $LIMIT ) {
# Split it
my @words = split(" ",$line);
my $frame = "";
my $count = 0;
foreach my $word ( @words ) {
$count += (length($word));
if( $count > $LIMIT ) {
$new_lines[$new_ind] = $frame . "/\n";
$new_ind++;
$frame = "";
$count= (length($word) + 1);
}
$frame .= ($word . " ");
} # end foreach my $word
$new_lines[$new_ind] = $frame . "\n";
} else {
# Add it to new array
$new_lines[$new_ind] = $line;
}
$new_ind++;
} # end foreach $lines
# Write new lines into the old file
close( FILE);
open(FILE,">xyz.txt");
print FILE @new_lines;
close(FILE);
print "\nDone";
I didn't really understand the question. What do you mean by?
a.abc file contains
file:b.abc
value:4
file:c.abc
value:6 ....
>>read a.abc file and get the file name and check that file is exist or not if file is exit read the value and print ..
When a.abc is read, we get file b.abc and there's a value 4 associated..so we print 4 ?
And if that's the case, when we get c.abc (also with a.abc.. should the o/p be 6 or should the other file be opened, with d.abc and e.abc inside?
P.S: I think we can do with less confusing filenames.
No need to use a stack. Keep another array which will tell the greatest element corresponding to the elements of the input array.
Use a spl variable to_set: which will point to the position of the array for which greatest element is yet to be found.
Is somewhat O(n), definitely not O(n2).
#include<stdio.h>
#include<stdlib.h>
#define N 4
void next_greatest(int a[])
{
int i;
// holds next greatest elements corresponding to a
int b[N];
// holds the first position for which next greatest has to be set
int to_set=0;
// initialize b[N] to -1
for( i=0; i< N; i++ )
b[i] = -1;
// find next greatest
for( i=0; i< N; i++ )
{
while( to_set < i )
{
if( a[i] > a[to_set] )
{
b[to_set] = a[i];
to_set++;
}
else
{ break;
}
}
} // end for
// print elements
for( i=0; i<N; i++ )
{
printf("\n%d -> %d", a[i], b[i]);
}
} // end next_greatest
int main()
{
int a[N] = { 4,5,2,25 };
next_greatest(a);
return 0;
}
OUTPUT:
4 -> 5
5 -> 25
2 -> 25
25 -> -1
Given input range limits: low and high (both inclusive).
Assume that you are generating numbers of length, say N.
Have a driver function that sets the value of the first number, first 'low' upto 'high'. then call a recursive function that does +1 and -1 to the previous value.
Code here [tested]:
#include<stdio.h>
#include<stdlib.h>
#define N 4
int low = 3;
int high = 6;
void spl_number( int a[], int pos )
{
if( pos == N )
{
printf("\n");
for( int i=0; i< N; i++ )
printf("%d ",a[i]);
}
else
{
if( a[pos-1]-1 >= low )
{
a[pos] = a[pos-1] - 1;
spl_number(a,pos+1);
}
if( a[pos-1]+1 <= high )
{
a[pos] = a[pos-1] + 1;
spl_number(a,pos+1);
}
} // end else
} // end spl_number
void driver(int a[])
{
for( int num=low; num<= high; num++ )
{
a[0]=num;
spl_number(a,1);
}
}
int main()
{
int a[N] = { 0 };
driver(a);
return 0;
}
#define N 4
int a[N][N] = { {1,2,3,4},
{12,13,14,5},
{11,16,15,6},
{10,9,8,7},
};
void print_num()
{
int i=0,j=0,count=0;
while( count < N*N )
{
// direction -> RIGHT
while( j <= N-1-i )
{
printf("%d ", a[i][j]);
j++; count++;
}
j--; i++;
// direction -> DOWN
while( i <= j )
{
printf("%d ", a[i][j]);
i++; count++;
}
i--; j--;
// direction -> LEFT
while( j >= N-1-i )
{
printf("%d ", a[i][j]);
j--; count++;
}
j++; i--;
// direction -> UP
while( i > j )
{
printf("%d ", a[i][j]);
i--; count++;
}
i++; j++;
} // end while
} // end print_num
Traverse the node in this way: Right sub tree, Left sub tree, Root.
Every time you reach the node, remove it from the tree and push it into a Stack.
At the end, the root node of the tree will be the top of the Stack.
Now start popping elements from the Stack and check if it falls within the specified range. If yes, then pass it to a method which constructs a BST.
C++ code here:
#include<stdio.h>
#include<stdlib.h>
int st_row,st_col;
int end_row, end_col;
int min_path_length = 99999;
int a[6][6]= { {0,1,0,0,1,0},
{1,1,0,1,1,1},
{0,1,1,1,0,1},
{1,1,1,0,0,0},
{1,0,0,1,1,1},
{0,1,1,1,0,0},
};
#define N 6
int is_valid(int row, int col, int val)
{
return (a[row][col] && ( a[row][col]==1 || a[row][col] > val ));
} // end is_valid
int is_start(int row, int col)
{
return (row==st_row) && (col==st_col);
} // end is_start
void find_path( int row, int col )
{
// Check if border
if( row ==0 || row == N-1 || col == 0 || col == N-1 )
{
if( a[row][col] < min_path_length )
{ min_path_length = a[row][col];
end_row = row;
end_col = col;
}
return;
}
int new_len = a[row][col]+1;
// top ?
if( is_valid(row-1,col,new_len) && ! is_start(row-1,col) )
{
a[row-1][col] = new_len;
find_path(row-1, col);
}
// bottom
if( is_valid(row+1,col,new_len) && ! is_start(row+1,col))
{
a[row+1][col] = new_len;
find_path(row+1, col);
}
// left
if(is_valid(row,col-1,new_len) && ! is_start(row,col-1) )
{
a[row][col-1] = new_len;
find_path(row, col-1);
}
// right
if( is_valid(row,col+1,new_len) && ! is_start(row,col+1) )
{
a[row][col+1] = new_len;
find_path(row, col+1);
}
} // end find_path
void trace_path ( int r, int c )
{
printf("\n%d %d", r,c);
if(r== st_row && c==st_col)
return;
//top
if( r!=0 && a[r-1][c] == (a[r][c]-1) )
{
trace_path(r-1,c);
}
//bottom
else if( r!=N-1 && a[r+1][c] == (a[r][c]-1) )
{
trace_path(r+1,c);
}
//left
else if( c!=0 && a[r][c-1] == (a[r][c]-1) )
{
trace_path(r,c-1);
}
//right
else if( c!=N-1 && a[r][c+1] == (a[r][c]-1) )
{
trace_path(r,c+1);
}
} //end trace_path
void print_matrix( )
{
printf("\nPrinting matrix..");
for( int i=0; i< N; i++ )
{
printf("\n");
for(int j=0; j<N; j++ )
{
printf("%d ", a[i][j]);
}
}
} //end print_matrix
int main()
{
print_matrix();
st_row=2; st_col =3;
find_path(st_row,st_col );
print_matrix();
printf("\nTracing path..");
trace_path(end_row,end_col);
return 0;
}
RepLoriKetron, Accountant at Aspire Systems
I am a content marketing professional at HubatSpot, an inbound marketing and sales platform that helps companies attract visitors, convert ...
Seems like a good solution.
- D October 31, 2013