• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interesting interview programming problem  RSS feed

 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have a java program(Read only) and lets name it A.java, and this program is compiler error free. Now lets make certain changes to it(delete/comment) some logic and print statements and store it as B.java.

Since B.java is a new program there are possibility for compilers errors in it. Now without compiling B.java we should figure out if there are any syntax errors in it.

Clue: Your program can take only B.java and find the solution (or) both A.java and B.java do some comparison (or) other logic and should print there is no error (or) error in the new program.

I was told that this is just 15 - 20 lines of code.
 
Alan Parson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class CTest {

public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec("javac B.java");
p.waitFor();
System.out.println(p.exitValue() == 0 ? "no error" : "error");
}
}

15/20 lines seems like a lot.
 
Alan Parson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure that there is some Apache/third party library out there that turns this problem into a one liner, but assuming we use only the classes that come with the JDK, and given the fact that B.java can be significantly different from A.java and not just a tweak of A.java. I don't see how you could write a lexer/parser from scratch in 15/20 lines (remember can't use ANTLR or javacc)

Is there a regular expression that covers Java syntax?
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short and smart code.

Thanks Alan Parson.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does that satisfy the requirement of not compiling B?
It is an interesting problem, and I am curious what a legitimate solution would look like.
 
Parth Bhatt
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Rusty Shackleford has a point...


Since B.java is a new program there are possibility for compilers errors in it. Now without compiling B.java we should figure out if there are any syntax errors in it.



The constraint set by Vishnu says that B.java cannot be compiled & the program suggested by Alan does just that...
 
Hanuma Deepak Muvvala
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rusky u r correct the above mentioned solution using process class may not be correct way ,in the question they mentioned using A.java , so there should be some mechanism to check whether we can compile a java program r not.....
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, is there another tool that checks syntax but does not generate byte code? JavaDoc?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
JavaDoc?


Brilliant!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!