ba127004
BAN USER- 0of 0 votes
AnswersGiven a binary tree return the new binary of given depth.
- ba127004 in United States
Example:
0 - 3 -- Should return full binary tree from root to height 3
3 - 4 -- Should return full binary tree from height 3 to 4| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer
With design questions, ask your interviewer in order to clarify, do not start writing code.
Of course, we will be using OOP model for that. But what kind of design pattern, should we implement : Singleton / Factory. It depends on the questions and it is not a bad idea to ask interviewer if he prefers this or that.
Who is going to use it? Any Special Cases -- Child, Adult, Older , Disable Type
What are the materials used? -- Wood, Steel, Iron, Vintage
Where are the objects to be used for? Office, Home, Outside, --
What are the objects ? -- Couch, Chair, Bed, Tea table, Desk, ---
--------------------------------------------------------------------
public class differentObjects extends forWhom {
string [ ] furnitures; //collection of different furnitures
//different furniture items : chair, bed, desk --
private int furnitureNumber ;
//constructor getting number of furnitures
public differentObjects(int numberOfFurniture) {
furnitureNumber = numberOfFurniture;
}
public void get_names () {
string [] furnitures = new furnitures[furnitureNumber]
furnitures [0] = chair
furnitures [0] = "chair";
furnitures [1] = "bed";
furnitures [2] = "desk";
....................................
.....................................
}
}
abstract class forWhom {
public for_home() {.........}
public for_office() {.........}
public for_special() {.........}
public for_child() {.........}
public for_adult() {.........}
public for_old() {.........}
}
//color of different furniture
public class colors extends differentObjects{
public void colors_available() { ...}
public void colors_out_of_stock () {..........}
public void colors_custom_made () {..............}
}
//materials made up of
public class materials extends differentObjects {
public void is_steel() {...}
public void is_iron() {...}
publlic void is_wood() {......}
public void is_vintage() {.............}
}
-------------------------------------------------------
//Here is the c++ version.
#include <iostream>
#include <list>
#include <iomanip>
#include <cmath>
#include <stdlib.h>
using namespace std;
/* Heap Sort Methods*/
void heapSorting(double arr[], int);
void heapify(double arr[], int);
void siftDown(double arr[],int, int);
int main(){
list<int> x_coordinate; //x co-ordinate list
list<int> y_coordinate;//y co-ordinate list
int i,j;
int count=0;
cout<<"Please do not enter comma between co-ordninate"<<endl;
cout<<"Enter 99 99 to exit"<<endl;
for(;i != 99, j !=99;)
{
cout <<"Enter co-ordinate "<<count+1 << " = ";
cin>> i >> j;
cout<<endl;
if(i == 99 || j ==99) {;}
else{
x_coordinate.push_back(i);
y_coordinate.push_back(j);
count++;
}
}
//iterators
list <int>::iterator x;
list <int>::iterator y;
int size = x_coordinate.size();
double arr[size];
int k =0;
//calculating square root
// the value is storedin array arr
for(x = x_coordinate.begin(),y = y_coordinate.begin(); x != x_coordinate.end() && y != y_coordinate.end();
x++,y++)
{
arr[k] = sqrt((*x * *x) + (*y * *y));
k++;
}
//calling heapsorting
heapSorting(arr, size);
int nearest_point;
cout << "Enter k (number of nearest points from origin) >> ";
cin >> nearest_point;
cout<<endl;
for(int t = 0; t < nearest_point; t++ )
{
cout<< fixed << setprecision(2) << arr[t] << " ";
}
cout<<endl;
return 0;
//iterators
list <int>::iterator x;
list <int>::iterator y;
int size = x_coordinate.size();
double arr[size];
int k =0;
//calculating square root
// the value is storedin array arr
for(x = x_coordinate.begin(),y = y_coordinate.begin(); x != x_coordinate.end() && y != y_coordinate.end();
x++,y++)
{
arr[k] = sqrt((*x * *x) + (*y * *y));
k++;
}
//calling heapsorting
heapSorting(arr, size);
int nearest_point;
cout << "Enter k (number of nearest points from origin) >> ";
cin >> nearest_point;
cout<<endl;
for(int t = 0; t < nearest_point; t++ )
{
cout<< fixed << setprecision(2) << arr[t] << " ";
}
cout<<endl;
return 0;
//iterators
list <int>::iterator x;
list <int>::iterator y;
int size = x_coordinate.size();
double arr[size];
int k =0;
//calculating square root
// the value is storedin array arr
for(x = x_coordinate.begin(),y = y_coordinate.begin(); x != x_coordinate.end() && y != y_coordinate.end();
x++,y++)
{
arr[k] = sqrt((*x * *x) + (*y * *y));
k++;
}
//calling heapsorting
heapSorting(arr, size);
int nearest_point;
cout << "Enter k (number of nearest points from origin) >> ";
cin >> nearest_point;
cout<<endl;
for(int t = 0; t < nearest_point; t++ )
{
cout<< fixed << setprecision(2) << arr[t] << " ";
}
cout<<endl;
return 0;
}
//----------------------------------------
void heapSorting(double arr[], int size){
heapify(arr, size);
int last = size - 1;
while(last > 0)
{
//swap the root(max value) of heap with last element
swap(arr[last], arr[0]);
last -= 1;
//maintaining max-heap order
siftDown(arr,0, last);
}
}
//-----------------------------------------
void heapify(double arr[], int size){
//index as a last parent node
int start = (size -1)/2;
while (start >=0){
siftDown(arr, start, size-1);
start -=1;
}
}
//------------------------------------------------------
void siftDown(double arr[],int first, int last)
{
int root = first;
int child;
while(root * 2 + 1 <= last)
{
child= root * 2 + 1;
int to_swap = root;
if(arr[to_swap] < arr[child])
{
to_swap = child;
}
if(child+1 <= last && arr[to_swap] < arr[child+1])
{
to_swap = child+1;
}
if(to_swap!= root )
{
swap(arr[root], arr[to_swap]);
root = to_swap;
}
else
{
;
}
}
}
//selecting siblings
- ba127004 March 10, 2012select *from person where
momid = 'given_momid' and
dadid = 'given_dadid' and
id != 'given_id';