Saturday, November 10, 2012

Access Modifiers in Java

Read some notes online, but just to summarize here for later convenient look-up.

There are four levels of accessibility in Java: public, protected, package, and private. From the blog in JavaPapers.com, I am also summarizing in a table below to show the differences.

Access LevelSame ClassSame PackageSubclassesOther Packages
publicYYYY
protectedYYYN
packageYYNN
privateYNNN

Besides, below lists some notes about the access modifier usage and default level.
  1. Classes can be qualified by public or no modifier only. When qualified without any modifier, by default the access level is package.
  2. Interfaces can be qualified by public or no modifier only. When qualified without any modifier, by default the access level is also package.
  3. Class methods and fields can be qualified by all, public, protected, private, or no modifier. When qualified without any modifier, by default the access level is package.
  4. Interface methods and fields can be qualified by public or no modifier only. When qualified without any modifier, by default the access level is public.
  5. Inheritance in Java can be public only and this is the default. Unlike C++, there is no protected or private inheritance.
  6. The access specifier for an overriding method can allow more, but not less, access than the overridden method. For example, a protected instance method in the superclass can be made public, but not private, in the subclass.

References
JavaPapers.com
TutorialsPoint
Default access modifier of interface
Inheritance in Java?
Java Tutorial

Sunday, October 21, 2012

Is the Number A Power of Two?

Problem:
Given an integer number, decide if it's a power of 2. Return true if yes, otherwise return false.

Analysis:
Can the number be negative?
>> Should not be. Since a power of any number can only be positive. Hence, let me assume non-negative numbers only in this problem.

Solution 1:
Runs in constant time and makes use of bit operations.

Solution 2:
Shifts the bits and counts the number of 1's.

Solution 3:
Uses arithmetic operation only. This is similar to Solution 2. Division by 2 is equivalent to right shift by 1 bit. Check the modulo by 2 is equivalent to check the least significant bit of the integer.

Binary Tree Comparison

Problem:
Given two binary trees, compare if they are the same. If yes, return true, otherwise return false.

Analysis:
This is a very common programming interview question. Not that hard but it's very good to test the idea of recursion. Also, there are some edge cases one needs to consider.

Complexity:
Since it's a binary tree, should consider average case and worst case.
Balanced or unbalanced?

Solution:

Sunday, October 14, 2012

Language Setting on Windows

Keep some notes about language setting on Windows. Just in case forget some time in the future.

After installing a English version Windows, to properly display foreign languages on the system, one may need to do following 2 steps:
  1. Install the language from the system CD.
    Normally, Control Panel -> Regions and Languages -> ...
    After this step, one may be able to see characters in the languages you just installed, like on webpages.
  2. Change language for non-Unicode programs.
    For some special programs, one may still see weird characters even after doing Step 1. This is because some programs were not coded in Unicode and in this case, changing the setting will help.

Note:
  • System restart is required for the changes to be effective.
  • One may notice some language changed even where you didn't expect (because of non-Unicode coding).

Saturday, October 6, 2012

Lowest Common Ancestor in Binary Search Tree Given Two Values

Problem:
Given a Binary Search Tree (BST) and two values, find the lowest common ancestor.

Note:
Two values are given in this problem, instead of two nodes. But the idea should be similar. Here I am giving two options: one is to find the node first and then search by node, and the other is to directly search by value. However, these two are the same in nature: comparing by value and validating the nodes.

Assumption:
The values are distinct in the BST. If not, the problem will become complicated. Consider the BST with all equal values in nodes.

Complexity:
Average case? Worst case?
For BST, balanced or unbalanced? Need to take this into consideration.

Solution 1:
Find the nodes first and then search by node.


Solution 2:
Directly search by value and then validate the values.