Oracle Interview Question for Java Developers


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
0
of 0 vote

After researching the above problem can be solved via Composite Design Pattern Easily..

- rameshelworthy July 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

while ((line = br.readLine()) != null) {
			Matcher m = Pattern.compile("(/*[\\w]+)").matcher(line);
			if (line.startsWith("[") && m.find()) {
				// read tags
				if (line.contains("/")) {
					// end tag
					if (line.contains(list.get(list.size() - 1))) {
						list.remove(list.size() - 1);
					} else {
						// Got an element with /, but it is not current
						break;
					}
				} else {
					//start tag
					list.add(m.group(0));
				}
			} else {
				// split data
				String[] keyVal = line.split("=");
				System.out.println(list.toString().replace("[", "").replace("]", "").replace(", ", ".")	+ "." + keyVal[0] + " Output " + keyVal[1]);
			}
		}

- Anonymous July 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Were you required to construct an Employee java object here which I think needs reflection knowledge. If that is not the case I would put everything in a map starting with the main map called employee. Then start putting everything as key value until I see an input like [address] for which I will create another map and save this as a nested map inside the main map and so on.

On retrieval, if I get employee.name - I will turn it into employee.get("name") and check if it instance of String, if it is - just output it, If not - it will be instance of Map and I can in that case continue until I find the String I am looking for.

I agree we can take help from the Composite pattern.

- Asif Garhi September 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String fileName = "configuration.conf";
Pattern KEY_MATCH = Pattern.compile("\\[.*\\]");
BufferedReader br = new BufferedReader(new FileReader(new File(fileName)));
String line;
String prefix = "";
while ((line = br.readLine()) != null) {
if(KEY_MATCH.matcher(line).matches()) {
String newPrefix = line.replaceAll("(\\[)(.+?)(\\])", "$2");
if(prefix.length() == 0 )
prefix = newPrefix;
else
prefix += "." + newPrefix;
} else {
String[] keyPair = line.split("=");
System.out.println(prefix + "." + keyPair[0] + " Output " + keyPair[1]);
}

}

- Hive November 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. var prefix = "";
2. var readline <-- read new line from file and repeat from step 3 to ....
3. if readline contain pattern [] then
3.1 var readline= readline without [ and ]
3.2 var prefix = prefix+(prefix.size==0 ? "" : ".")+readline;
4 else if readline contain pattern [/] then set prefix = substr(prefix,0,last index of "." - 1)
5 else
5.1 str[] = split readline on "="
5.2 var key = prefix + "." + str[0]
5.3 var value = str[1]
5.4 stroe key value pair into hashmap so that value can be retrived from hashmap on given key like employee.name

- avi.jasu01 February 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Node
{   private HashMap<String, Node> Child = new HashMap<String, Node>(4);
    private HashMap<String, String> Values = new HashMap<String, String>(4);
    private String name;
    private Node parentNode;
    public String getName()
            
    { return name;}
    public void setName(String name)
    {name = name;}
    public Node getParentNode()
    {return parentNode;}

    public void setParentNode(Node parentNode)
    { this.parentNode = parentNode;}

    public HashMap<String, Node> getChild()
    {return Child;}

    public void setChild(HashMap<String, Node> child)
    { Child = child;}

    public HashMap<String, String> getValues()
    {return Values;}

    public void setValues(HashMap<String, String> values)
    {Values = values; }
}

public static void main(String[] args)
    {
        Node root = new Node();
        Node currentNode = root;
        try
        {

            File f = new File("C:\\Users\\Documents\\oracle.txt");
            BufferedReader b = new BufferedReader(new FileReader(f));
            String readLine = "";
            while ((readLine = b.readLine()) != null)
            {
                if (Pattern.matches("\\[[^\\]]*\\]", readLine))
                {
                    System.out.println(readLine);
                    if (readLine.contains("/"))
                    {
                        currentNode = currentNode.getParentNode();
                    }
                    else
                    {
                        Node temp = new Node();
                        temp.setName(readLine);
                        temp.setParentNode(currentNode);
                        currentNode.getChild().put(readLine, temp);
                        currentNode = temp;
                    }
                }
                else
                {
                    String[] out = readLine.split("=");
                    currentNode.getValues().put(out[0], out[1]);
                }
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

- rameshelworthy June 19, 2018 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More