Twitter 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

Looking for help on interview preparation?
Visit AONECODE.COM for ONE-TO-ONE private lessons by FB, Google and Uber engineers!

Customized course covers
System Design (for candidates of FB, LinkedIn, AMZ, Google and Uber etc)
Algorithms (DP, Greedy, Graph etc. every aspect you need in a coding interview and Clean Coding)
Interview questions sorted by companies
Mock Interviews

Our members got into G, U, FB, Amazon, LinkedIn, MS and other top-tier companies after weeks of training.

Feel free to email us aonecoding@gmail.com with any questions. Thanks!


SOLUTION
The push, pop and inc can all take place in O(1) time
if there is an additional array to maintain the m incremented at each position n.

class Stack:
    def __init__(self):
        self.nums = []
        self.add = []
    
    def push(self,num):
        self.nums.append(num)
        self.add.append(0)
        print num," "
        
    def pop(self):
        try:
            number_to_add = self.add.pop()
            print self.nums.pop() + number_to_add
            if self.add:
                self.add[-1] += number_to_add
        except:
            print "can't pop from an empty stack"
            
    def inc(self, n, m):
        if not self.nums:
            print "empty"
        if n > 0:
            n = min(n, len(self.nums))
            self.add[n - 1] += m
        print self.nums[-1] + self.add[-1], " "

- aonecoding January 04, 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