Tuesday, 31 December 2013

First of all it's almost time for new year. So wishing everyone a Very happy New Year! May all your wishes come true. And may Linux and FOSS have more amazing next year.

2013 was an amazing year for Linux and it is just not possible to list them all. However I am attempting to list a few ones here.

ANDROID

Year 2013 marked a record of Android phone activation. The figure is 1.5 Million devices per day. Over 900 million Android devices have been activated since being introduced in 2008 and this figure is set to hit 1 billion soon. Not to mention behind Android as an OS there is Linux kernel running.

Raspberry pi

One of the greatest development ever in the history of Low cost, single board computer was Raspberry pi.
Raspberry pi was intended to promote Linux computing in schools and
elsewhere and the board was highly welcomed by the FOSS Community and
still continuing.

Linux in Space

Manager of the Space Operations Computing (SpOC) for NASA Keith Chuvala said , "We migrated key functions from Windows to Linux because we needed an operating system that was stable and reliable -- one that would give us in-house control. So if we needed to patch, adjust, or adapt, we could." Distribution used was Debian,

SteamOS

2013 was indeed a great and marvelous year for Linux gaming.SteamOS, a debian based distribution was designed for Stream Machine Game Console and released in the mid of December 2013. With the trend of GNU/Linux into gaming environment is certainly a very welcome act.

The Firefox OS

Firefox OS (project name: Boot to Gecko, also known as B2G) is a Linux-based open-source operating system for smartphones and tablet computers.It was released in late April 2013. The ARM based Linux distribution for mobile devices, shows promising future.

Ubuntu Touch

Canonical released Ubuntu Touch 1.0, the first developer/partner version on 17 October 2013, along with Ubuntu 13.10 that "primarily supports the Galaxy Nexus and Nexus 4 phones.

Chromebooks

Chromebooks wins the market of notebook computers, with a lot of high-end manufacturer viz., Samsung, ASUS giving place to GNU/Linux OS over Proprietary OS’s.

Kali Linux

From the developers of BackTrack Linux comes Kali Linux.
Kali is a Linux distribution based on Debian, the mother OS which is
Primarily developed for Penetration testing and shares a lot of
repository of Debian, one of the most rich Distro. Kali Linux holds the
record download, in a very less time of its release.

Android Kitkat

One of the Most awaited release was named Kitkat. Google Announced Android 4.4 aka KitKat in September of 2013. Although the release had been expected to be number 5.0 aka Key Lime Pie. Kitkat has been optimised to run on a large variety of devices having a minimum of 512 MB RAM.

Miscellaneous

Linux was not only constrained to desktops, tablets and smartphones but was also used in automobiles, space station, robots etc. 2013 was indeed a great year for Linux and the coming years we may see further boost.

Some memorable Linux milestones

Sunday, 29 December 2013

A very basic interview question asked. In an earlier post we had see the LinkedList class in java and how to reverse it using Collections.revere(). You can refer to that post. Point to note that the LinkedList class defined in java is infact a doubly Linked list and that is not what the interviewer will be interested in. Yes you can give that as your 1st answer as it will depict you have java knowledge but at some point you will have to fall back to basics.

Before we write the code to actually reverse the LinkedList lets write the code for the LLNode data structure which will form our LinkedList.

What is Android Debug Bridge?

Android Debug Bridge (adb) is a versatile command line tool that lets you communicate with an
emulator instance or connected Android-powered device.

It is a client-server program that includes
three components:

A client, which runs on your development machine. You can invoke a client from a shell
by issuing an adb command. Other Android tools such as the ADT plugin and DDMS also create
adb clients.

A server, which runs as a background process on your development machine. The server
manages communication between the client and the adb daemon running on an emulator or device.

A daemon, which runs as a background process on each emulator or device instance.

A client can be you adb shell that you run or your IDE(Eclipse). As for the server you can check the status, kill it or start it through ADB command line tool utility. To check the daemon process you can simply type adb in your android terminal(if rooted).

To get the overview of ADB refer to following picture

Where can I find ADB tool utility?

You can find the adbtool in <sdk>/platform-tools/. You can download the SDK from here.

More on ADB....

When you start an adb client, the client first checks whether there is an adb server
process already running. If there isn't, it starts the server process. When the server starts,
it binds to local TCP port 5037 and listens for commands sent from adb clients—all adb
clients use port 5037 to communicate with the adb server.

The server then sets up connections to all running emulator/device
instances. It locates emulator/device instances by scanning odd-numbered
ports in the range 5555 to 5585, the range used by emulators/devices.
Where the server finds an adb daemon, it sets up a connection to that
port. Note that each emulator/device instance acquires a pair of
sequential ports — an even-numbered port for console connections and an
odd-numbered port for adb connections. For example:

As shown, the emulator instance connected to adb on port 5555 is the same as the instance
whose console listens on port 5554.

Once the server has set up connections to all emulator instances, you can use adb commands to
access those instances. Because the server manages connections to emulator/device
instances and handles commands from multiple adb clients, you can control any emulator/device
instance from any client (or from a script).[Source]

Usage Common Usage Examples

adb device(shows all the devices with device id the adb server is connected to)

adb help( Shows adb version and all possible commands)

adb start-server(To start adb server if not started already)

adb kill-server(To kill adb server)

adb push <local> <remote>(To push a file from local machine to remote device/emulator)

adb pull <remote> <local>(To get a file from remote device/emulator to local machine)

adb version(Shows version number of adb running)

adb reboot(Reboots the device)

adb root(Restarts the adbd daemon with root priveledges)

adb backup(Used to backup your device. You can backup everything with -all argument)

Wednesday, 25 December 2013

Question : Find next right node of a given key

Given a Binary tree and a key in the binary tree, find the node right
to the given key. If there is no node on right side, then return NULL. Expected time complexity is O(n) where n is the number of nodes in the given binary tree.
For example, consider the following Binary Tree. Output for 2 is 6, output for 4 is 5. Output for 10, 6 and 5 is NULL.

Output :

Right Node of node with value 10 is : nullRight Node of node with value 2 is : Data is : null and Value is : 6Right Node of node with value 6 is : nullRight Node of node with value 8 is : Data is : null and Value is : 4Right Node of node with value 4 is : Data is : null and Value is : 5Right Node of node with value 5 is : null

Monday, 23 December 2013

Question :

Given a Binary Tree, replace the data of each node by the sum of data of all its descendent nodes.(Leaf nodes will have 0)

Solution :

The solution is simple recursive one. For each node you need to set value of the node equal to the sum of value of it's left and right node. Each sub node will in turn calculate their value and based on sum of their children and return the sum with it's original value added.

Before providing the solution lets view the problem graphically

Before we process the tree lets say it is something like below

The final result we are interested is

Lets write the code for it. Note the code for Node data structure as well as various traversals is provided in this post which was poster earlier. There is a slight modification that we are adding and int value instance variable along with its getters ans setters. There is corresponding change in the constructor but the logic remains same.

Sunday, 22 December 2013

Question 1

Four people need to cross a rickety bridge at
night. Unfortunately, they have only one torch and the bridge is too
dangerous to cross without one. The bridge is only strong enough to
support two people at a time. Not all people take the same time to cross
the bridge. Times for each person: 1 min, 2 mins, 7 mins and 10 mins.
What is the shortest time needed for all four of them to cross the
bridge?

Answer

The initial solution most people will think of is to use the fastest
person as an usher to guide everyone across. How long would that take?
10 + 1 + 7 + 1 + 2 = 21 mins. Is that it? No. That would make this
question too simple even as a warm up question.

To reduce the amount of time, we should find a way for 10 and 7 to go together.

Question 2

A guard is positioned at the one side of bridge say 'A'.
* His task is to shoot all those who try to leave from 'A' to other side say 'B'.
* He also need to welcome the person who come from other side 'B' to his side 'A'.

The guard comes out of his post every 1 hour and looks down the bridge for any people trying to leave.
Monica a brilliant girl is at side 'A' and wish to go to other side 'B'. She also know's it would take her 1:45 hr to cross the river.

She comes with an super idea and able to cross the river.How did Monica cross the river ?

Answer

Steps
1. Monica go from side A to B for approx 59 minutes
2. Lisa then move back to side A for 2 minutes to fool guard. Now guard will think she is coming to side A.
3. then she again move toward B

Question

How would you cut a rectangular cake into two equal pieces when a
rectangular piece has already been cut out of it? The cut piece can be
of any size and orientation. You are only allowed to make one straight
cut.

Possible Solution

The simplest solution that one might think of is to cut the cake horizontally along the height of the cake. That would divide the cake in exact two equal pieces.This solution won't work if there is some frosting on the top which also needs to be divided.

Other possible solution would be to make a cut such that it passes through center of both the rectangles. Since the cut halves both the rectangles, the resulting two pieces are guaranteed to have equal area.

Android has OpenGL ES which is embedded version of OpenGL(2D/3D graphics).

SQLite database for structured data storage.

Android versions

Cupcake (1.5)

Donut (1.6)

Éclair (2.0/2.1)

Froyo (2.2)

Gingerbread (2.3)

Honeycomb (3.0/3.1/3.2)

Ice Cream sandwich (4.0)

Jelly Bean (4.1/4.2/4.3)

Kitkat (4.4)

Lollipop (5.0/5.0.2)

Application Fundamentals

Applications are written in Java programming language.

You compile applications into Android package file(.apk files) which you can then distribute.

Each application run in its own sandbox protected and isolated from other. They also run in their own Linux process.

Application consists of Components, a Manifest file and resources.

Android Components

Activities

Represents a single screen with a user interface.

An application consists of multiple Activities.

When new Activity starts, the old one is pushed onto stack which user can navigate by pressing back key.

Can be built with xml files or directly by Java.

Services

Used to perform long running operations in the background.

No interface.

For example playing music. No matter what you are doing music keeps playing once started.

Can be bound to other application.

Content Providers

Used to store and retrieve data and make it accessible to all the devices.

By default there is no way to share data across different applications.

Exposes public URI to others application.

Uses database model.

Example Contacts, Media etc.

Broadcast receivers

Responds to system wide broadcast announcements

Example when screen turns off Android sends broadcast which your application can listen to. Other example is when battery gets low.

You can also broadcast your own messages which other applications can listen to.

No user interface though they can be used to create status bar notifications.

Android Manifest File

Name must be AndroidManifest.xml and must be in Applications root directory.

Gives Android System information about the Application.

Describes components(mentioned above like Activities) used in Application.

Declares permission required to run the application. If you must have notices when you try to install any app from the market store you have to give some permissions. These are given here.

Declares minimum API level. If this is set only people with Android running version more than the API level will be able to view the application on the app store.

Android Lifecycle

Android Lifecycle Methods

onCreate() :This methods is called when activity is created.

First call will be to super.onCreate() so that android will do some of it's own initializations.

You can set the activity's content view by calling method setContentView().

You can get references to various UI components like EditText, Button etc by calling findViewById() method and then attach various listeners to it. Eg you can set onClickListener for button.

onRestart() : This method will be called when activity is stopped and is about to start again. If there is any logic that you need to execute on activity restart you should add it here.

onStart() : [Visible only behavior] This method is called when the activity is about to start. In this method you should typically add logic to load persistent application state like reading from database.

onResume() : [Foreground only behavior ] This method is called when activity is visible and is about to start interacting with the user. In this method you should do tasks related to activity coming to foreground. For eg. Starting a playback music or starting some animation etc.

onPause() : Opposite of onRsume(). This method is called when activity is about to loose focus. In this method you should do things like stopping animation or stopping playback music... handle things you started in onResume() method. You should also save any data that you need to persist like storing user entered data in database.

onStop() : This is when activity is no longer visible to the user. In this method you should typically cache your activity state in case activity is killed and needs is restarted later.

Note : If activity is killed by Android OS then onStop() is not called. So saving any persistent data should be done in onPause() method.

onDestroy() : This method is called when activity is about to be destroyed. Typical things to do here is release resource hold by your application.

Note : Again if activity is killed by Android OS then onDestroy() is not
called. So saving any persistent data should be done in onPause()
method.

Thursday, 19 December 2013

Very basic interview question : Swap two variables without using third or temporary variables. This serves the purpose of getting started with the interview. There are 3 ways to do this and we will discus all of them now

Monday, 16 December 2013

Background

You must have heard of binary trees. Nothing special about it. Each node has two nodes(right and left). One of the most common interview questions is traversing the tree. There are three common types of traversals -

Pre Order (Root - Left - Right)

In Order (Left - Root - Right)

Post Order (Left - Right - Root)

We are going to first write code for the data structure that forms the Node which is used further to build the tree. Node class code goes something like below

Very simple. In this node structure we have String data and two nodes right and left. We have corresponding getters and setters. Note that data,right and left are instance variables and are assigned default values(null is our case).

Level Order

Leaving the above three types of traversal there is one more type of traversal called level traversal in which we visit all nodes on the same level from left to right before moving on to the next level.

But you cannot determine a unique binary tree with just it's Pre ordee and Post order traversals. However if you have the constraint that each internal node has both two children then you can very well find the original tree with above data.

Wednesday, 11 December 2013

We know we have java.util.Stack as a data structure in Java. It has standard functions like pop(), push(), peek(). But ever wondered what is analog for Queue in Java?

If you search for Queue class in java then you will wind such an interface java.util package.

public interface Queue<E> extends Collection<E>

And if you check classes implementing this interface you will find LinkedList in it. Yes LinkedList in java can be used for FIFO operations. Actually LinkedList implements Deque which inturn implements Queue. If you see functions in Queue interface they are as follows -

element(): This method retrieves the head of the queue.

offer(E o): This inserts the specified element into the queue.

peek(): This method retrieves the head of this queue, returning null if this queue is empty.

poll(): This method retrieves and removes the head of this queue, or return null if this queue is empty.

There are two ways to find power of a number. One is iterative which is very simple(Just multiply the number power number of times) and other is recursive. Following is the code to get power of a number in recursive way.

Tuesday, 3 December 2013

The phone's internal memory (not the SD card) is solid-state (flash)
memory, AKA NAND. It can be partitioned much like a normal hard drive
can be partitioned. The bootloader exists in its own partition. Recovery
is another partition; radio, system, cache, etc are all partitions.

Here are the standard partitions on an Android phone:

/misc - not sure what this is for.

/boot - bootloader, kernel

/recovery - holds the recovery program (either clockworkmod or RA recovery for a rooted Evo)

During the rooting process, a critical piece of the process is disabling
a security system built into the bootloader that protects these
partitions from accidental (or intentional) modification. This is
what's referred to as "unlocking NAND." The security system can be set
to active or inactive. S-ON means the security is in place (NAND
locked). S-OFF means the security is off (NAND unlocked). When S-OFF,
you have the ability to modify all partitions. With S-ON, you only have
write access to /cache and /data. Everything else is read-only.

When you flash a custom ROM, that ROM typically includes a kernel and an
OS. That means the /boot and /system partitions will be modified at a
minimum. Some ROMs require a clean install, so a format of the /data
and /cache partitions is sometimes built into the .zip that you flash.
This is essentially doing a factory reset. See next paragraph.

When you do a factory reset (AKA: wipe, hard reset, factory wipe, etc.),
you are erasing the /data and /cache partitions. Note that a factory
reset does NOT put your phone back to its factory state from an OS
standpoint. If you've upgraded to froyo, you will stay on froyo, because
the OS lives in /system, and that is not touched during a factory
reset. So "factory data reset," as it says under Settings > SD &
phone storage, causes confusion. It's not a factory reset. It's a
factory DATA reset. Now you know the distinction.

The SD card can also be partitioned to include a section dedicated to
storing user apps. To create the partition, your SD card needs to be
formatted. Typically a user will copy all the contents in the SD card to
a PC hard drive, wipe the card and partition it, and then copy
everything back.

Onto kernels....

A kernel is a layer of code that allows the OS and applications to
interface with your phone's hardware. The degree in which you can
access your phone's hardware features depends on the quality of code in
the kernel. The homebrew (rooting) community for HTC has made several
kernel code improvements that give us additional features from our
hardware that the stock kernel does not. When you flash a custom ROM,
you automatically get a kernel. But you can also flash a standalone
kernel ROM on top of the existing one, effectively overwriting it. These
days, the difference in custom kernels is less about new features and
more about alternate configurations. Choosing a custom kernel is
basically choosing one that works best with your ROM.

Kernel developers are typically responsible for a small, specific
feature in the kernel. For example, netarchy's contribution to custom
kernels mainly revolve around removing the framerate restriction that
was present in the stock HTC kernel. However, because of the
open-source philosophy of all the devs, each kernel "distribution"
contains the work of several devs. They openly share new code that they
deem will benefit everyone. Basically the best features stay in, and
the distributor of the kernel will give credit where credit's due. This
means that for any custom kernel you try, you might be using netarchy's
FPS fix. It's all a team effort. A new improvement will make it to
most if not all of the various kernel distributions in a short amount of
time.

For the Evo, the hot features of custom kernels are:

a fix to remove the 30fps cap imposed by HTC and improve touch
tracking sensitivity (HTC finally fixed this in their stock kernel late
last year)

disabling perflock to enable CPU throttling. Great for increasing the performance of your phone and/or improving battery life

iptables firewall to enable wifi-tethering via the wifi-tether app

3 or 5 point multitouch support (not too many practical applications)

HAVS, a control system that dynamically adjusts the voltage based on
CPU load. This has proven to be a battery saver, but it can actually
have the opposite effect when multiple control systems are operating
(like setCPU).

BFS kernel task scheduler as an alternative to the standard CFS

SBC (the ability to charge your battery beyond the default safe
limit). The concept is similar to overclocking a processor: you're
overriding the safety limits established to achieve additional
performance. The benefit here is that you may gain more use of your
battery per charge. The drawback is that you can damage the battery and
significantly reduce its longevity. Some kernels claim they are using a
safe technique to prevent battery damage. Just be aware of the
potential risks.

Note about 4G and HDMI:
AOSP ROMs (ROMs based on pure Android without 3rd party manufacturer's
code) are currently lacking robust 4G and HDMI support. This is because
4G and HDMI are hardware-specific features, not Android-specific, so
there is no existing kernel code in the Android Open Source Project
(AOSP) that is specific to 4G or HDMI support.

HTC is the manufacturer of the phone, so it has intimate knowledge of
the hardware it uses in the phone; therefore the kernel that HTC has
written does support HDMI and 4G. The problem is that the kernel is
also tied in with their Sense UI that it's not possible to simply
extract the portions of the code governing 4G and HDMI. Therefore,
Sense-based ROMs support 4G because they simply borrow the existing HTC
code. Meanwhile AOSP ROMs have to write their own code for 4G and HDMI
in order to support it. Their ability to do this depends on how much
info they can obtain about the hardware components in the phone and any
drivers they may be able to find. Otherwise, it's really a task of
reverse-engineering the hardware, which can be a thankless, time
consuming task.

Note : This is not my original writing. This is picked up from android forum. I just though the knowledge would be more readable for beginners here than on the forums.