Google Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

struct RoomLog {
    let log: String
}

var log = [
    RoomLog(log: "E 111"),
    RoomLog(log: "E 222"),
    RoomLog(log: "L 111"),
    RoomLog(log: "E 111"),
    RoomLog(log: "L 222"),
    RoomLog(log: "L 111"),
]

func isLogWellFormed(_ log: [RoomLog]) -> Bool {
    var set = Set<String>()
    for item in log {
        if item.log.hasPrefix("E ") {
            let key = item.log.replace("E ", with: "")
            if set.contains(key) { return false } // alrady entered
            set.insert(key)
        }
        else if item.log.hasPrefix("L ") {
            let key = item.log.replace("L ", with: "")
            set.remove(key)
        }
        else {
            return false// incorrect log record
        }
    }
    return true
}

extension String {
    
    func replace(_ str: String, with newString: String) -> String {
        return self.replacingOccurrences(of: str, with: newString)
    }
}
print("well formed: \(isLogWellFormed(log))") // well formed: true

- seriyvolk83 April 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create a hash table, go through each element and:
1) if starts with 'E' insert if such element isn't there, otherwise return invalid error
2) if starts with 'L' remove element with a key 'L {number}', otherwise return invalid error
Finally, if there are any keys left, that means building isn't empty, return invalid list error

- Marcello Ghali April 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In an interview with this simple semi-open question, you'd better mention as much detail as possible.
Marcello thought about three corner cases, while there are more to mention
- The room should not be infinite large, ask the interviewer for a maximum size and check the size when someone enters to make sure there isn't too many people in the same room.
- What if one of the person in the room throw his badge out of window and someone else get it, then enter the room with the badge, it will simply trigger invalid log in the above algorithm. It is better to ask then simply making default assumption that there is no such a case. Because in reality, it is totally possible.

- nobody December 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In an interview with this simple semi-open question, you'd better mention as much detail as possible.
You thought about three corner cases, while there are more to mention
- The room should not be infinite large, ask the interviewer for a maximum size and check the size when someone enters to make sure there isn't too many people in the same room.
- What if one of the person in the room throw his badge out of window and someone else get it, then enter the room with the badge, it will simply trigger invalid log in the above algorithm. It is better to ask then simply making default assumption that there is no such a case. Because in reality, it is totally possible.

- nobody December 03, 2017 | 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