• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interesting interview programming problem

 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short and smart code.

Thanks Alan Parson.
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.....
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm, is there another tool that checks syntax but does not generate byte code? JavaDoc?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stan James:
JavaDoc?



Brilliant!
 
reply
    Bookmark Topic Watch Topic
  • New Topic