Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

class BoolStack {
    var top = true  // doesn't matter when empty
    var stack = [Int]()

    var isEmpty: Bool { stack.count < 1 }
    
    func push(_ input: Bool) {
        if !isEmpty && top == input {
            stack[stack.count - 1] += 1
        } else {
            top = input
            stack.append(1)
        }
    }
    
    func pop() -> Bool {
        let result = top
        if !isEmpty {
            stack[stack.count - 1] -= 1
            if stack[stack.count - 1] < 1 {
                stack.removeLast()
                top.toggle()
            }
        }
        return result
    }
    
    func empty() -> Bool { isEmpty }
    func clear() {
        stack = []
    }
}

- Anonymous January 31, 2022 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@dataclass
class TrueRun:
    start: int
    end: int

class BoolStackSparse:
    def __init__(self):
        self.clear()

    @property
    def _top(self) -> TrueRun:
        return self.true_runs[-1]
    
    def push(self, value: bool):
        """ Push a bool onto stack """
        if value:
            # Need to push a run or update
            if self._top.end == self.head_idx:
                self._top.end += 1
            else:
                self.true_runs.append(TrueRun(self.head_idx, self.head_idx+1))
        self.head_idx += 1
    
    def pop(self) -> bool: 
        """ Pop in lifo order """
        if self.empty():
            raise ValueError("empty")
        v = False
        if self._top.end == self.head_idx:
            # We are in a true run
            v = True
            self._top.end -=1
            # If the top true run has 0 len, pop it (unless it's the run at 0)
            if self._top.end == self._top.start and self._top.start != 0:
                self.true_runs.pop()
        self.head_idx -= 1
        return v
    
    def empty(self) -> bool:
        """ Whether the stack is empty """
        return self.head_idx == 0
    
    def clear(self) -> None:
        """ Reset to empty state """
        self.head_idx = 0
        self.true_runs = [TrueRun(0, 0)] # List of TrueRun insts

- Anonymous November 04, 2023 | 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