Here will discuss the array implementation of stack.
Problems with array implementation
Underflow - Array may be empty but people may try to pop the element
Overflow - Array is full. To over come we have to use re-szing of array. We can see how we can solve this problem here - http://k2code.blogspot.com/2013/09/resizing-array-implementation-of-stack.html
Null items - Can nulls be added - Yes in this case, nulls can be added in stack
[Read More]
Array implementation of stack
Here will discuss the array implementation of stack.
Problems with array implementation
Underflow - Array may be empty but people may try to pop the element
Overflow - Array is full. To over come we have to use re-szing of array. We can see how we can solve this problem here - http://k2code.blogspot.com/2013/09/resizing-array-implementation-of-stack.html
Null items - Can nulls be added - Yes in this case, nulls can be added in stack
[Read More]
int atoi( char* pStr )
Problem: Write the definition for this function without using any built-in functions. if pStr is null, return 0. if pStr contains non-numeric characters, either return 0 (ok) or return the number derived so far (better) (e.g. if its “123A”, then return 123). assume all numbers are positive. plus or minus signs can be considered non-numeric characters. in order to solve this program, the programmer must understand the difference between the integer 0 and the character ‘0’, and how converting ‘0’ to an int, will not result in 0.
[Read More]
Reverse a String
Problem: A typical programming interview question is “reverse a string, in place”. if you understand pointers, the solution is simple. even if you don’t, it can be accomplished using array indices. i usually ask candidates this question first, so they get the algorithm in their head. then i play dirty by asking them to reverse the string word by word, in place. for example if our string is “the house is blue”, the return value would be “blue is house the”.
[Read More]
The Circular Lake Monster Problem
Problem: For #1, how about row in a circle a bit smaller 1/4r in size. Since he won’t be able to keep up with you, when he is on the opposite shore you can make a brake for it. You have r*3/4 to travel, but 4*3/4 is less then pi, so he won’t be able to catch you in time
Solution: Assume x is the monster’s speed. Then to get the circle trick to work, you row a circle a little less than 1/x of the radius, leaving you 1 - 1/x to row when he is opposite of you.
[Read More]
Arrays tip : Ignoring the zeroth row
This trick can be used in any language but is shown in java right now.
Sometimes you want to use natural input values as an index, the real values that that data has instead of starting with zero. Let’s take the case of data that starts with the value 1, like the day of the month. The standard approach is to subtract one from every day value that’s used as an index.
[Read More]
Enums in java
In prior releases, the standard way to represent an enumerated type was the int Enum pattern:
**// int Enum Pattern - has severe problems!** public static final int SEASON\_WINTER = 0; public static final int SEASON\_SPRING = 1; public static final int SEASON\_SUMMER = 2; public static final int SEASON\_FALL = 3; But these are not type safe, and clearly naming them is bit of a problem. So in Java 5, they introduced Enums.
[Read More]
Modifying Java Variables (w.r.t c and c++)
Modifying Simple Variable
The only mechanism for changing the value of a simple Java variable is an assignment statement. Java assignment syntax is identical to C assignment syntax. As in C, an assignment replaces the value of a variable named on the left- hand side of the equals sign by the value of the expression on the right- hand side of the equals sign.
Modifying Object Variable Java object variables can be changed in two ways.
[Read More]
Constructor in java 1
Constructors When you create a new instance (a new object) of a class using the new keyword, a constructor for that class is called. Constructors are used to initialize the instance variables (fields) of an object. Constructors are similar to methods, but with some important differences.
Constructor name is class name. A constructors must have the same name as the class its in.
Default constructor. If you don’t define a constructor for a class, a default parameterless constructor is automatically created by the compiler.
[Read More]
Common naming convention : Coding Style
**Variable names must be in mixed case starting with lower case. **
Common practice in the Java development community and also the naming convention for variables used by Sun for the Java core packages. Makes variables easy to distinguish from types, and effectively resolves potential naming collision as in the declaration
eg.
int state;
Names representing constants (final variables) must be all uppercase using underscore to separate words.
MAX_ITERATIONS, COLOR_RED
[Read More]