I could not resist temptation of being interviewed by a company using GridGain and Hadoop in production. The position turned out to be somewhat different from the common set of Java engineer responsibilities but the interview did not.
Strangely enough for a heavily data-centric role (and they are pretty much competing with GOOG in local search on mobile devices) they did not care about algorithms whatsoever. Moreover, not all interviewers were even from the hiring team (sic!).
A) Imagine that you need to implement unoptimized but reliable char[] merge(char[] c1, char[] c2)so that, for example, [a,b] and [c,d,e] become [a,c,b,d,e]. So it's like merge-sort only easier. Basically, all it takes is
final int total = c1.length + c2.length;
while (k < total) {
if (i<c1.length) c[k++] = c1[i++];
if (j<c2.length) c[k++] = c1[j++];
}
and for the life of me I could not see what was wrong with my solution. Hint: imagine that both input array are of sizes larger than (Integer.MAX_VALUE / 2).
B) When a finally clause will not be executed? A tricky question because the answer is System.exit()
Strangely enough for a heavily data-centric role (and they are pretty much competing with GOOG in local search on mobile devices) they did not care about algorithms whatsoever. Moreover, not all interviewers were even from the hiring team (sic!).
A) Imagine that you need to implement unoptimized but reliable char[] merge(char[] c1, char[] c2)so that, for example, [a,b] and [c,d,e] become [a,c,b,d,e]. So it's like merge-sort only easier. Basically, all it takes is
final int total = c1.length + c2.length;
while (k < total) {
if (i<c1.length) c[k++] = c1[i++];
if (j<c2.length) c[k++] = c1[j++];
}
and for the life of me I could not see what was wrong with my solution. Hint: imagine that both input array are of sizes larger than (Integer.MAX_VALUE / 2).
B) When a finally clause will not be executed? A tricky question because the answer is System.exit()
C) I am not sure whether it was funny or stupid but I could not remember precisely whether finalize() is public or even declared on the Object class (as opposed to, say, something weird like Serializable::writeObject). We also discussed: why object resurrection is dangerous (the method is called only once) and whether making "this" unavailable in this method would help (no because it would be impossible to access instance state and so clean anything at all).
D) A legitimate but not realistic questions about application of identity operator (frankly, I did not remember about the cache inside of Integer):
new String ("abc") == "abc"
String.valueOf("abc") == "abc"
Integer.valueOf("100") == Integer.valueOf("100")
Integer.valueOf("1000") == Integer.valueOf("1000")
E) A textbook example of interview questions I really hate (just imagine something like that in production code) - guess in which order it will print if you call new Parent() and new Child():
class Parent {
Parent() {
System.out.println(getFoo());
System.out.println(getBar());
}
public int getFoo() {
return foo;
}
public static int getBar() {
return bar;
}
private int foo = 1;
private static int bar = 2;
{
System.out.println("P-1");
}
static {
System.out.println("P-2");
}
}
class Child extends Parent {
Child() {
System.out.println(getFoo());
System.out.println(getBar());
}
public int getFoo() {
return foo;
}
public static int getBar() {
return bar;
}
private int foo = 10;
private static int bar = 20;
{
System.out.println("C-1");
}
static {
System.out.println("C-2");
}
}
D) A legitimate but not realistic questions about application of identity operator (frankly, I did not remember about the cache inside of Integer):
new String ("abc") == "abc"
String.valueOf("abc") == "abc"
Integer.valueOf("100") == Integer.valueOf("100")
Integer.valueOf("1000") == Integer.valueOf("1000")
E) A textbook example of interview questions I really hate (just imagine something like that in production code) - guess in which order it will print if you call new Parent() and new Child():
class Parent {
Parent() {
System.out.println(getFoo());
System.out.println(getBar());
}
public int getFoo() {
return foo;
}
public static int getBar() {
return bar;
}
private int foo = 1;
private static int bar = 2;
{
System.out.println("P-1");
}
static {
System.out.println("P-2");
}
}
class Child extends Parent {
Child() {
System.out.println(getFoo());
System.out.println(getBar());
}
public int getFoo() {
return foo;
}
public static int getBar() {
return bar;
}
private int foo = 10;
private static int bar = 20;
{
System.out.println("C-1");
}
static {
System.out.println("C-2");
}
}
2 comments:
why would
Integer.valueOf("100") == Integer.valueOf("100");
Integer.valueOf("100") == Integer.valueOf("1000")
be true?
That's the beauty/insanity of the question. Just look at the source code - you are not guaranteed to get a new instance:
valueOf(int i) {
public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) {
return IntegerCache.cache[i + offset];
}
return new Integer(i);
}
Frankly, I think this question is pretty ridiculous - I doubt many people out there use identity instead equality for classes. Not to mention digging into the standard library code deep enough to notice the cache.
Post a Comment