First Round of Google Phone screen interview backup - virgoboy2004的专栏 - 博客频道 - CSDN.NET
Question 2: s.l(1 2 3 4) = 4s.l.(1 4 5 6 9) = 3s.l.( 1 3 5 7 9) = 1s.l.(1 2 10 11 12 13)
Read full article from First Round of Google Phone screen interview backup - virgoboy2004的专栏 - 博客频道 - CSDN.NET
qeustion 1: Write a program that reads in a text file, and only outputs the unique lines:
(can't use unix uniq command)
Input:
A
B
A
C
Output:
A
B
C
- public void readFile(File file) {
- if(file == null)
- return;
- BufferReader reader = null;
- FilerReader fileReader = null;
- try{
- fileReader = new FileReader(file);
- reader = new BufferReader(fileReader);
- String line= reader.nextLine();
- Set<String> hasSet = new HashSet<String>();
- while(line != null) {
- if(!hasSet.contains(line)){
- hasSet.put(line);
- System.out.println(line);
- }
- line = reader.nextLine();
- }
- }
- finally{
- if(reader.isOpen())
- reader.close();
- if(fileReader.isOpen())
- fileReader.close();
- }
- }
Question 2: s.l(1 2 3 4) = 4s.l.(1 4 5 6 9) = 3s.l.( 1 3 5 7 9) = 1s.l.(1 2 10 11 12 13)
- int straightLen(int[] cards) {
- if(cards == null)
- return 0;
- int length = 1;
- int longest = 0;
- ing next = Integer.MaxValue;
- for(int i = 0; i < cards.length; i++){
- if(next == cards[i]) {
- length++;
- }
- else{
- length = 1;
- }
- if(longest < length){
- longest = length;
- }
- next = cards[i] +1;
- }
- return longest;
- }
<database>
<store id=”MTV”>
<address>1600 Amphitheater Pwky.,
Mountain View, CA 94043</address>
<orders>
<order id=”12345”>
<status>backordered</status>
<items>
<item>Nexus 5</item>
<item>Hello Kitty Phone Case</item>
</items>
</order>
<order id=”ABCDE”>
<status>delivered</status>
<items>…</items>
</order>
…
</orders>
</store>
<store id=”SFO”>…</store>
…
</database>
public abstract class XMLParser {
public XMLParser() {
…
}
public void Parse(InputStream input)
{
// Implemented by the library, contains
calls to StartElement,
// EndElement and Content.
…
}
// Implemented by you.
protected abstract void StartElement(String
tag,
Map<String,
String> attributes);
protected abstract void EndElement(String
tag);
protected abstract void Content(String
text);
}
public class DataBase{
public
List<Store> getStores(){
}
}
public class Store {
public
String id;
private
List<Order> orders;
public
Store(String id){
this.id
= id;
}
public List<Order> getOrders(){
return
orders;
}
}
public class Order {
public
String id;
private
List<Item> items;
public
Order(String id){
this.id
= id;
}
public List<Item> getItems(){
return
items;
}
}
public class Item{
public
String name;
public
Item(String name){
this.name
= name;
}
}
public enum ELEMENTS{DATABASE(“database”),
STORE(“store”), ORDER(“order”), ITEM(“item”)}
public class MyXMLParser extends XMLPareser
{
Database
database = null;
Map<String,
Store> storeMap = new TreeMap<String, Store>();
Stack<Object>
stack = new Stack<Object>();
protected
void StartElement(String tag,
Map<String,
String> attributes) {
ELEMENTS
elTag = new ELEMENTS(tag);
switch(elTag){
DATABASE:
database
= new DataBase();
stack.push(database);
break;
STORE:
String
id = attributes.get(“id”);
Store
newStore = new Store(id);
stack.peek().add(newStore);
storeMap.put(id,
newStore);
stack.push(newStore);
ORDER:
String id = attributes.get(“id”);
Order newOrder = new Order(id);
stack.peek().add(newStore);
stack.push(newOrder);
ITEM:
Item
newItem = new Item();
}
}
protected
void EndElement(String tag) {
stack.pop();
}
protected
void Content(String text) {
if(isXMLTag(text)){
StartElement
}
}
private
boolean isXMLTag(String text){
//TODO
}
private
}
http://tutorials.jenkov.com/java-xml/sax-example.htmlRead full article from First Round of Google Phone screen interview backup - virgoboy2004的专栏 - 博客频道 - CSDN.NET