## PKT

BAN USER- 0of 2 votes

AnswersClass A

- PKT in India

{ void print(){}

}

Class B extends A

{ void print(){}

public static void main(String args[])

{

B b = new A();

}

}

Whats wrong with the above piece of code?

will below mentioned line work?

B b = (B)new A();| Report Duplicate | Flag | PURGE

Citigroup Java - 0of 0 votes

AnswersYou have given a number, rearrange the digits of given number and find the next large number.

- PKT in India

For example given number is 2576

the next large number is 2657

code is not required approach or algo is enough.| Report Duplicate | Flag | PURGE

Amazon Applications Developer - 0of 0 votes

AnswersGiven:You have given Amazon's stock prices for next 10 days

- PKT in United States

Find out: max benefit in best time complexity to buy and sell 1 share

Condition: Share must be sold any day after buying date.

For ex:

Share in thousands

5 1 4 6 7 8 4 3 7 9

Max benefit 9 - 1 = 8 thousand| Report Duplicate | Flag | PURGE

Amazon Applications Developer Algorithm - 0of 0 votes

AnswersGiven a non sorted array consisting 0's and 1's. find the index of first '1'. write a complete program which takes less time complexity. and test all boundary conditions also.

- PKT in United States

Eg: If given array is 0,0,0,1,0,0,0,1,1,1,1 the out put should be 3.| Report Duplicate | Flag | PURGE

Problem Solving

Given:

Matrix [M x N]

Logic:

-Have 4 variable columnMin, columnMax, rowMin, rowMax

-Initialize variables as

columnMin = 0

rowMin = 0

columnMax = N

rowMax = M

Loop

if(columnMin != columnMax)

{

Print Row from Matrix [rowMin][columnMin] to Matrix [rowMin] [columnMax]

rowMin ++ ;

}

else

{

break;

}

if(rowMin != rowMin)

{

Print Column from Matrix [rowMin][columnMax] to Matrix [rowMax][columnMax]

columnMax - - ;

}

else

{

break;

}

if(columnMin != columnMax)

{

Print Row from Matrix [rowMax][columnMax] to Matrix [rowMax][columnMin]

rowMax - - ;

}

else

{

break;

}

if(rowMin != rowMin)

{

Print Column from Matrix [rowMax][columnMin] to Matrix [rowMin][columnMin]

columnMin ++ ;

}

else

{

break;

}

END LOOP

Minor Doubt:

most frequently occurring number means same number is repeating in Binary Search Tree.

Here my doubt is if we have 10,10,10 to insert in BST

we will insert first 10 as a root

then will process second 10 and will match with first 10

10 not< 10

10 not>10

so we can not insert second 10 in left or right as per these 2 above rule

Is there any rule for 10==10 to insert in BST ???

Let 3 engineers are A B C

Let A's salary is 15L

He told randomly 20 L

like wise B and C told Salary and sum is 50L

now A is subtracting 5L and now sum is 45 L

B and C both knows below two points:

---A told 20 L initially

---previous sum = 50 L and after A's correction it is 45 L

Is A's salary still hidden from B and C ????

Logic:

1) Have all the subset of array separated by 0 for analysis. [because having zero in ant subset will make product 0]

2) Now in all subset of non-zero numbers follow below steps:

2.1) In case number of negative element is even find product of all numbers and keep this product in a global variable to compare with other products

2.2) In case odd numbers of negative elements divide subset into two more subset separated by first negative element

Parallely divide subset into two more subset separated by last negative element

---In this way we will get 4 possible products for a subset get the Max and proceed for further sub-set analysis.

-Have an array to hold tree node pointers

-copy root pointer at mid point of into array

-------------10-------------------------

-now replace existing tree node with its left and right if exists

--------------5-11---------------------

-again replace existing tree node with its left and right if exists

--------------9-20-15-------------------

--------------14-25----------------------

-----------------30------------------------

-at each steps we can print tree node info value alternately

[i/p] : [5, 11], [6, 18], [2, 5],[3,12]

Logic:

-Sort input birth component and death component separately

birthSortedList = [2,3,5,6]

deathSortedList = [5,11.12.18]

-Now we will perform a kind of merge sort where we have two sorted list of births & deaths and in resultant array we will enter char '{' in case birth is selected and '}' in case death is selected.

resultantArray = { { { } { } } }

-now have a counter,MaxLivingAnimalEndYearIndex and MaxLivingAnimal(initialized with zero) and start traversing resultant array and increase counter with 1 for each '{' and decrese it for each '}'

-In case (counter > MaxLivingAnimal) copy counter into MaxLivingAnimal and copy current index into MaxLivingAnimalEndYearIndex

counter = 2

MaxLivingAnimalEndYearIndex = 3 (indicate index in resultantArray)

-At last we will have MaxLivingAnimal and MaxLivingAnimalEndYearIndex. Just before MaxLivingAnimalEndYearIndex we will have MaxLivingAnimalStartYearIndex .

MaxLivingAnimalEndYearIndex = 3 (indicate index in resultantArray)

MaxLivingAnimalStartYearIndex =2 (indicate index in resultantArray)

-Traverse resultantArray upto index 'MaxLivingAnimalstartYearIndex' and count no. of '{' upto' resultantArray[MaxLivingAnimalstartYear]' that will give you index of 'MaxLivingAnimal start Year' in birthSortedList

-Traverse resultantArray upto index 'MaxLivingAnimalEndYear' and count no. of '}' upto 'resultantArray[MaxLivingAnimalEndYearIndex]' that will give you index of 'MaxLivingAnimal End Year' in birthSortedList

-We have Start and end year for max animal alive.

In case sorting is allowed:

sort the array in ascending order

possible pattern we will get like below:

1. + + ........................ + + +

2. - - .......................... - - +

3. - - .......................... + + +

4. - - .......................... - + +

5. - + .........................+ + +

6. - - ......................... - - -

For 1st, 5th and 5th case product of last three number(most +ve) will be highest

For case 2nd and 4th product of most -ve two number and max positive number will be highest

For case 3 find [product of most -ve two number and max positive number] and [product of last three number(most +ve)] and which ever is max will be max in all.

Logic:

recursion is one of the best solution for this problem

but yes it will be considered as 2 traversal of input list

because

one time traversal means one you analysed the the node and moved out don't come back to that node again.... whereas in our case one time we check whether the node is not null to get end point for node and second time we analysed the node to perform adding operation.

so 2 traversal..... it seems :)

Logic:

-Traverse array

-check each char for 'a' or 'b'

-in case found 'b' delete it and continue traversing

-in case found 'a' check for next char

-in case after 'a' next char is not 'b' continue

-in case after 'a' next char is 'b' check for this 'b' next char

-in case after 'a' next char is 'b' and 'b's next char is not 'c' then delete 'b' and continue traversing

- in case after 'a' next char is 'b' and 'b's next char is 'c' then delete 'abc' and continue traversing

conclusion:

search for occurrence of 'a' or 'b' in case 'b' comes first delete it or check whether 'abc' pattern is there.... if yes.... delete it.

Logic:

-Search the first occurrence of element from left side say elementLeftIndex

-Search the first occurrence of element from right side say elementRightIndex

-now [elementLeftIndex-elementRightIndex+1] is the max length as asked in qus

-apply logic for all elements

@Algo: just a corner case:

suppose there are 38 elements in array but some how we dont know the limit of array.

u found index = 16 to 32 and element not found

now u tried to make range 33 to 64 but element array[64] doesn't exist

how efficiently would you go back to element array [38]

one possible case handling I think is:

in case array [64] not present go for mid of 32 and 64 for MaxSearchRange= 48

array [48] also doesn't exist the go for array [40]... and so on...

Approach:

As mentioned in qus we have to find customer who returns to Amazon site(any page)....

so we should focus on Amazon site customer only.....

all those log entry which describe for non -Amazon site we should ignore.

now there are two case on page ID:

1. page id is kind of numeric, alphanumeric or something we can't just say its Amazon's site page without checking Amazon site page IDs list

2. page id is always containing something common so that we can figure it out its Amazon's site page for eg: pkt.amazon.com/index , pkt.amazon.com/review

-For any of case we will sort out only Amazon pages visited by customer

-Pick a customer Id and calculate date from given timestamp

-Find out next occurrence for that customer id in logs and check for visited date from timestamp

-In case there is no diff in date find for next entry and remove one entry from log list

-In case till the end we dont find the customer visit in >= 1 day remove that customer visit entry

-In case there is diff of at least one day in both log entry data then make a entry of the customer in Result list and remove all the occurrence of that customer from sorted log list so that it wil be easy to do search in log list for other customers.

-Now pick another customer and apply logic as above....

Logic for a kind of optimization:

-sort the array (nlogn)

-select first element say x

-now you have to find element which is (target - x)

-traverse upto [element<=(target-x)]

-in case met (target-x) return true

-else select second element and repeat the above procedure

-in case no (target-x) found return false

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

Open Chat in New Window

@PKTSOngChen:

- PKT July 04, 2013Just to understand the question,

if the input is {1,5,7,8,9,45,78,2,4,1,7,12,23,34,6,23,45};

The increasing sub-arrays are

{1,5,7,8,9,45,78} -> Longest and should be the result.

{2,4}

{1,7,12,23,34}

{6,23,45}

The output should be {1, 5, 7, 8, 9, 45, 78}

Wt else you got from qus ???