Friday, February 14, 2014

Can you store all of the possible (Java) byte numbers on your laptop memory?

Q- Can you store all of the possible (Java) byte numbers on your laptop memory?
A- Probably yes. To build the answer, let's follow these steps:

1- How much memory 1 byte uses in Java?
8 bits (= 2^3 bits).
        Please check this post and this one on a new window.

2- How many possible representations of numbers in byte are available in Java?
256 (=2 ^ 8) possibilities, which can vary between -2^7 and +2^7 -1.
        Please check the reference on a new window.

3- How much memory is needed to store all the possible representations of numbers in byte?
2 K bits or 256 bytes  (256 x 8 bits = 2^8 * 2^3 = 2^[8+3] = 2^ 11 bits).
      
4- How much memory is available on your laptop?
I don't know, but you probably have enough!



http://interviewqa4java.blogspot.com/2014/02/how-many-bytes-are-in-1-gb.html

How many bytes are in 1 GB?

Q) How many bytes are in 1 GB?
We could approximate that 1 Gigabyte has 1 billion bytes, but the correct answer is  2^30 bytes.

How can I remember this answer in the future? Well here is one trick to always remember this.  Please read how to build the following table:
  1. Starting with 1 byte = 2^0 bytes, make a table multiplying the previous row by 2, or just annotate the next power of 2:   2^0, 2^1, 2^2, 2^3, etc.
  2. Once you reach 2^10 bytes, you can call it  1 KB (which is not 1,000, but 1024 bytes
  3. For 2^10 to 2^19, the table mirrors the first 10 rows, except for we add the unit K (Kilo). So 2^10 B is 1 KB, 2^11 B is 2 KB, 2^12 B is 4 KB, and so forth.
  4. Once you reach 2^20 bytes, you can call it  1 MB (which is not precisely 1million bytes, but 1024^2 bytes)
  5. The rule #3 is analog for 2^20 to 2^292^30 to 2^39, etc. So 2^25 B [= 2^(20+ 5) B] is 2^5 MB or  32 MB and 2^37 [= 2^(30+ 7) B] is 2^7 MB 128 GB.



Decimal
(Bytes)
Digital Storage (Bytes)
2^0
1
2^1
2
2^2
4
2^3
8
2^4
16
2^5
32
2^6
64
2^7
128
2^8
256
2^9
512
2^10
1 K (1024)
2^11
2 K
2^12
4 K
...
...
2^20
1 M (1024^2)
2^21
2 M
...
...
2^30
1 G (1024^3)
2^40
1 T

How many bits are in 1 byte?

Q-01) How many bits are in 1 byte?
In most of the cases, 1 byte refers to 8 bits.

Q-10) - How many possible numbers can be represented with 1 bit? 
Each 1 bit can hold 2 (= 2^1) possible values: 0 or 1.

Q-11) How many possible numbers can be represented with 1 byte? 
Each 1 byte (=8 bits) can hold 256 (= 2^8) possible values: 0, 1, 10, 11, 100, 101, 110, 111, 1000, ... , 11111111 .



Binary
Decimal
00000000
0
00000001
1
00000010
2
00000011
3
00000100
4
...
...
11111101
253
11111110
254
11111111
255

How do you implement multiple inheritance in JAVA?

How do you implement multiple inheritance in JAVA?
A1- First of all, Java doesn't allow you to have one class extending 2 or more classes. According to the problem you are dealing with, you can have your class to extend another class and implement many interfaces.

But let's say that you have 2 classes A and B already implemented from a library and you want  the class C to "extend from both". What can you do?

The solution A1 doesn't apply to this problem. So let's see other options:

A2- You can extend one class (e.g. C extends A) and write on your class C all non-private members from class B, redirecting them to class B members. E.g:

public class A{
  //...
}

public class B{
    public int bMehtod(int param) {  return (param + 123);   }
}

public class C extends A{
  private B b;

  public C() { this( new B() ); }
  public C(B b) {
       if ( b==null ){
           this.b = new B();
       } else {
            this.b = b;
       }
  }
  
//B members section
    public int bMehtod(int param) {  
return this.b.bMehtod(param);   //just forward the call to B
    }
}


Thursday, February 13, 2014

How would you sort a huge array of ages?

How would you sort a huge array of ages?

Consider you have a huge array of numbers that represent the ages of the employees in a company and you want to write an efficient algorithm to sort this array. How would you do and what is the Big O of this algorithm?

Answer: if you were sorting a regular array, you could just use the merge sort and have a great O(n . lg n) , but... Wait a minute... Can we have an even more efficient solution? Sure! This is a very specific set of data, that represents the employees ages. Since we know about the data scope, we can use this knowledge to get to an O(n) algorithm. Let's see how!

Developing the Solution:
  In the known real world ages can vary between 0 and 122, but let's make our solution effective up to the next few decades or more and assume the maximum value should be 150. Since we know that the huge array is a distribution of integer values within this range, we could easily
  1. Count in O(n) how many employees have each age between 0 and 150, with a single loop, storing the values in a temporary array (each index is an age and each value is the count)
  2. Overwrite in O(n) the input array values using the counts of each age,  since an array always has its indexes ordered!
Show me the code!
For the sake of time I've written the code in JavaScript. Feel free to write it in Java or you favorite language


//Global var
MAX_AGE = 150;

//Main
function sortAges(a /* unsorted array */){ if(! isValid(a)) return a; var c= countAges(a); /* temp array of age counts*/ for(var i=0, j=0; i< c.length; i++){ var count=c[i]; if(!count) continue ; for(var k=0; k<count; k++){ a[j+k]=i; } j=j+k; } return a /*sorted array of ages*/; }

//Helper
function countAges(a /*array of ages*/){ var c= new Array(MAX_AGE + 1); for(var i=0; i < c.length; i++){ c[i]=0; } for(var i=0; i< a.length; i++){ c[a[i]]++; } return c; }

//Validation
function isValid(a /* anything!*/){ if( typeof(a)!="array" && typeof(a)!="object") return false; if(!a.length) return false; try{ for(var i=0; i<a.length; i++){ var v=a[i]; if(typeof(v)!= "number") return false; //it doesn't handle decimals if(v < 0 || v > MAX_AGE) return false; } } catch(e){ return false; } return true; }

//Code to generate data for algorithm validation
function createRandomAgesArray(n /*new array length*/){ var a=[]; for(var i=0; i<n; i++){ a[i]= getRamdomAge(); } return a; }

function getRamdomAge(){ return Math.round( MAX_AGE * Math.random() ); }

//Generating data and testing
MAX_AGE=18; //making it simple to verify
var a= createRandomAgesArray(10); 
console.log(a);
sortAges(a);
console.log(a);
MAX_AGE=150; //change beck to maximum value