Hewlett Packard Interview Question for Java Developers
- -2of 2 votes
Answersclass NoName { children = new Hashtable() name = "" boolean hasChild(child: string) { return this.children.hasKey(child) } NoName addChild(child: string) { var childnode = new NoName() childnode.name = child this.children[child] = childnode return childnode } NoName getNode(child: string) { if this.hasChild(child) return this.children[child] else return this.addChild(child) } void addList(input: string) { var currentNode = this.getNode(input[0]) input = input.SubString(1, input.Length ? 1) if (input.Length < 1) currentNode.getNode("") else currentNode.addList(input) } string scan() { if (this.children.Values.Length == 0) return this.name if (this.children.Values.Length == 1) return this.name + this.children.Values[0].scan() var temp = new Array() foreach(child in this.children.Values) temp.Add(child.scan()) return this.name + "{" + temp.Sorted().JoinArray(",") + "}" } } var x = new NoName() x.addList(’/home/user/foo’) x.addList(’/home/user/bar’) x.addList(’/home/user/baz/one’) x.addList(’/home/user/baz/two’) print x.scan()
Is there anyone can help me translate these code to java or tell me the result ,I will really appreciate it.Thanks
- miller9977 January 10, 2014 in United States| Report Duplicate | Flag | PURGE
Hewlett Packard Java Developer Coding
Interview Type: Written Test