Tuesday, March 6, 2012

Good Flex Interview Questions [Flex Basics (Part 3)]


You can read updated part1and part2 here.

Question: What is the easiest way to achieve drag-and-drop behavior in list?
Answer: We can set dragEnabled, dropEnabled etc properties for list-based controls and if they contain same objects then it works smoothly. We can also control drag-and-drop mechanism using DragManager.



Question: How can we control drag-and-drop using custom drag methods.
Answer:Assume that we want to create an application where we are able to drag a pop-up into a bottom dock component  When a pop-up is dragged into botton-dock it will get minimized and take some action. So first thing its to provide drag support for the pop-up. We will add an event-handler for mouseDown event for the pop-up.

 private function onMouseDownHandler(event:MouseEvent):void  

           {  
                var dragLinkFromPopUp:IUIComponent = event.target as  
                 IUIComponent;  
                var dragSource:DragSource = new DragSource();  
                dragSource.addData(dragLinkFromPopUp, "LinkFromPopUp");  
                DragManager.doDrag(dragLinkFromPopUp,dragSource,event);  
           }  

Here our pop-up is the target for mouse-down event. We are also setting dragSource property becuase we want to put some condition for acceptDragDrop() by DragManager.
Then the next thing will be to accept the object when it enters into our BottomDock:

 addEventListener(DragEvent.DRAG_ENTER,onDockItemDragEnterHandler);  
                addEventListener(DragEvent.DRAG_DROP,onDockItemDropHandler);  
and write the function body as:

           private function onDockItemDragEnterHandler(event:DragEvent):void  

           {  
                if(event.dragSource.hasFormat("LinkFromPopUp"))  
                {  
                DragManager.acceptDragDrop(event.currentTarget as   
                IUIComponent);  
                }  
           }  
           private function onDockItemDropHandler(event:DragEvent):void   
           {  
                var dragItem:Object =   
                       event.dragSource.dataForFormat("LinkFromPopUp");  
                //Do your Processing.
           }  
We can read more about drag-drop here:

Coming Next:
More basic questions about:
Custom Component and Examples
ExternalInterface API and FA Bridge
Shared Object
and a separate post for about java-flex interaction, lcds/blazeDS, blazeDS Spring integration etc.

So Did you find this post helpful? What would you like to be improved/added? Please spent some time to provide your feedback that can help improve it.

Saturday, March 3, 2012

Tough and Good Logical Puzzles

In this post I have collected some of the logical puzzles asked by various IT companies. Some of them are answered here but for some of them I have no solution. In case you have a solution please share.

Question: Find the pattern in the following series (Oracle).
1, 11, 12, 1121, 122111,......
Solution:Basically this pattern shows the count of each number along with the number itself.
If we see how pattern is moving we notice:
1
There is only one 1 so it is 11.
11
There are two 1s so next one would be 12.
12
Now we have one 1 and one 2 so 11 21.
11 21
Now we have two 1s and one 2 and one 1 so 12 21 11
So we can proceed similar way.

Question: There are three buckets: containing oranges, apples and (apple + oranges). The labels of the buckets has got changed by mistake and none of the buckets has correct label? What is the best way to identify the correct items in all three buckets using one guess only? 
Solution:Take a piece of fruit from the box marked “apples and oranges”. Suppose the fruit you take is an apple. Then that box must be the box containing just apples. Therefore, the box marked “oranges” can’t be the box containing just apples, and it can’t be the box containing just oranges either – so it must be the box containing apples and oranges. The remaining box is therefore the box containing just oranges.’
If the fruit you take out is an orange, the solution is derived in a similar fashion: the box marked “apples and oranges” is the box containing the oranges; the box marked “apples” is the box containing both apples and oranges; the box marked “oranges” is the one containing just apples.




Question: There are ten balls and one of the balls is heavy. We have a Tarazu( two-pan balance scale) without weights and we need to use it minimum number of times to identify the heavy ball? How will you identify?
Solution: Easy One.
1.First make two groups of four balls each. Then put them on both sides.
2.If any of the sides has got more weight choose that group. Then repeat it using 2 balls in each pan and in the final attempt we will get the heavy ball. 
3. If weight is same in step2 then use the remaining 2 balls to weight and find the heavy one.

Question: We want to store the thirteen cards of one particular suit (A,2,3,....,J,Q,K) in one data structure. We can assign them number from one to thirteen if we want. We want to design a game where user will be allowed to take one card out and then we need to identify the card selected by user. What is the best strategy to design this game. (oracle)
Solution:We can store them as integers from 1,2,...13 and then we can also store their sum which will be equal to [13(13+1)]/2. When a user selects any of the card then again find the sum of remaining cards and substract it from the initial sum to get the number

Question: There are eight coin and one of them is deteriorated (note that it can be heavy or light but not eqaual). We have a balancer and we need to identify that culprit coin using the balancer minimum times? How many times will you use the balancer and what will be your strategy? (Adobe)
Solution: Answer is 3. Check out he following flow for comparisons to be performed.


Question: I am having ten cigarette packs and every pack contains 10 cigarettes. Every cigarette further weighs 10 grams, so every pack weighs 100 grams. But in one of packs every cigarette weighs 1 gram extra (i.e. 11 grams) and thus that pack weighs 110 grams. We don't know which pack is culprit. We have electronic balancer(not usual one with two panes) and we need to figure out that pack. How can we figure out? (HSBC Technologies)
Solution: Take one cigarette out of first pack, two cigarettes out of second pack, three cigarettes out of third pack, four out of fourth pack and so on. Then place all of them in the pan. In Ideal situation the weight should be
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
But if any of the count belongs to that faulty pack it will create that much amount of extra grams. For example of we took out 4 cigarettes out of fourth pack and that is the one which is faulted then it will increase the weight by 4 grams. So if the weight is extra by n grams then the nth pack is the one we need to find.

Question: We have a number of identical coins where number is more than 2. But one of the coins is fake but we do not know whether it is lighter or heavier. Design an algorithm to determine the coin when we have a Tarazu?
Solution: We can keep aside one coin if n is odd or two coins if it is even. Then we divide the coins (even in number) in two groups of same size and put them in the respective panes. If the weight is same then the problem is with the coins kept aside and we can weight them. If the former weighs less, the fake coin is lighter; otherwise, it is heavier. If the first weighing does not result in a balance, take the lighter group and, if the
number of coins in it is odd, add to it one of the coins initially set aside. Divide all these coins into two equal-size groups and weigh them. If they weigh the same, all these coins are genuine and therefore the fake coin is
heavier; otherwise, they contain the fake, which is lighter. Since the puzzle cannot, obviously, be solved in one weighing, the above algorithm solves it in the minimum possible number of weighings.We

Question. A temple has 3 lords. There is one magical river near to temple. If you dip flowers into this river they will get doubled. One person plucks some flowers, dips them into magical river, gets doubled and devotes some of these flowers to one of the deity. Then he comes out dips the flowers (remaining ones) again and then again goes into the temple and devotes some of them. Again comes out, dips and then devotes all of them this time. At last he observed that he has devoted equal number of flowers to each of them. How many flowers did he pluck and how many did he devote. (Wipro)
Solution:Let us assume person plucks x flowers and then dips them to get 2x and then offers y flowers to first lord. The he comes back with 2x-y flowers and dips them to get 2(2x-y) flowers and again offers y flowers and comes out with (4x-2y-y) flowers and then dips them to get 2(4x-3y) flowers.
Then again offers y flowers to third deity and what is left is (8x – 6y – y) but he is left with no flower. So we can say 8x-7y = 0. And set of values that satisfy this is x=7 and y=8. So he plucked 7 flowers at start and offered 8 flowers to each deity.

Question: Ram goes to market in morning and finds that one of the shopkeeper is selling 2 lemons for one rupee. He buys 30 lemons for 15 rupees. Shyam goes to market after sometime and observes that the rate has changed to 3 lemons for one rupee. He buys 30 lemons for 10 rupees. Then they merge their lemons and plan to sell 5 lemons for 2 rupee. At the end of day they realized they earned 24 rupees. But they actually invested 25 (10 + 15) rupees. So how come they have got a loss of one rupee? (Adobe)
Solution: Let us go to the pairing logic they implemented. If we create group of 2 lemons for Ram and group of 3 lemons for Shyam we get:
OO       OO       OO       OO       OO       OO       OO       OO OO OO       OO       OO         OO     OO       OO

Ram got 15 groups.
OOO     OOO     OOO     OOO     OOO     OOO     OOO     OOO OOO      OOO
Shyam got 10 groups
 But when they try to merge them to create group of 5 we notice:
OOOOO            OOOOO            OOOOO            OOOOO OOOOO            OOOOO            OOOOO            OOOOO OOOOO         OOOOO
So far so good.
OOOOO               OOOOO            
Here is the problem as ram bought for 2 lemons per one rupee means 5 lemons for 2.5 rupees but selling them for 2 rupee and will get a loss here for 50 paisa per group.
As he got two groups being sold at a loss of 50 paisa per group, there will be a loss of one rupee.

Question:  There are three bulbs at the top floor of a building and there are three switches at the ground floor. We can only go once to the top floor and we need to tell which switch belongs to which bulb. (Deloitte)
Solution: Switch on one of the switches (say switch A) and then after sometime switch it off. Then switch on another switch (say switch B) Then rush to top floor and we will see one of the bulb glowing that corresponds to switch B. Then touch the remaining two bulbs. The bulb which would be warm is the one corresponding to Switch A and third one corresponds to last switch.

Question: There are 1000 bulbs in a room and the switches for these are in another room arranged in a random fashion. You have to find an optimum strategy to match the bulbs to corresponding switches so that the number of items you enter into the room to look at the bulbs is minimum. (Amazon, Adobe)
Solution:

Question:There are 1000 wine bottles and one of them contains the poison. What is the minimum number of tries needed to find the bottle with a poison in it? (Adobe)

Question:A person shoots her wife. Then holds her under water for 5 minutes. Finally, he hangs her. But after 10 minutes they both go out together and enjoy a wonderful dinner together. How can this be
Solution: He was a photographer :).

Question: There are 25 horses. You have to find out the top five i.e. the fastest five of them. What is the minimum number of races you would conduct? (Adobe)

Other useful links: