I Failed a Twitter Interview

The deadline for confirming my return internship with Amazon was October 28th, but my friend Daniel convinced me that if I applied to Twitter I'd be able to finish all my interviews before then. So I went for it.

First they asked me to solve two Codility questions within a one hour time frame. The questions were interesting ("is this an anagram of a palindrome" and "count points of equilibrium in a 2d array"). I felt semi-confident about my solutions, but soon enough Judy, a lovely recruiter from Twitter, emailed me and set up a phone screening for 5:30 on Wednesday.

I don't know about you, but I get VERY nervous before interviews. I think it's mainly because I don't want the interviewer to think I'm stupid. So you can imagine, at 5:20 my desk was cleared, my notepad was labeled "Twitter Interview, Wed. Oct 23" and 2 perfectly sharpened pencils were ready for rigorous scribbling. Then came 5:30, and I started staring at my phone.

At 5:35 I googled "time in California" to make sure I calculated for the time difference right. Nope: according to Google it was 2:30 PST, 5:30 ET.

At 5:48 I emailed Judy, asking her to look into the situation. 10 minutes later I got a phone call from San Francisco. Judy apologized for the mess up and told me Justin is available to interview me right now.

Deep breath.

"Awesome, lets do it!"

Justin also apologized for the scheduling mistake, and quickly dove right into the programming question:

"Consider the following picture:"

alt text

"In this picture we have walls of different heights. This picture is represented by an array of integers, where the value at each index is the height of the wall. The picture above is represented with an array as [2,5,1,2,3,4,7,7,6]."

"Now imagine it rains. How much water is going to be accumulated in puddles between walls?"

alt text"We count volume in square blocks of 1X1. So in the picture above, everything to the left of index 1 spills out. Water to the right of index 7 also spills out. We are left with a puddle between 1 and 6 and the volume is 10."

_______

Side note for the curious reader: I posted a gist with the correct solution to this problem at the bottom. You can keep reading without spoilers :)

_____

The first thing I tried to do was to figure out how much water we would have at any given index. This stroke a resemblance with Calculus and integrals, so I immediately remembered that looking for local maximums could be of use. And indeed, in the picture above, the water above index 2 is bounded by the smaller of the two surrounding maximums at index 1 and 6.

I was thinking out loud: "What if we found all the local maximums, and filled in water between them. Would that work?"

"Yeah, that should work" replied Justin.

So I went ahead and coded this solution. Then Justin asked me for a bunch of test cases which I provided. All the test cases we talked about seemed to work.

"Do you have questions for me?" Justin asked. "How did I do?" "Reasonably well. Your solution does 2 passes, but there is a more interesting one that does only 1"

We then had a short chat about life at twitter.

The second I hung up I realized my solution was wrong.

Think about this input:

alt text

My solution solved between the local maximums and looked like this: 
alt text

But the result should have been one puddle between the two taller towers: 
alt text

The next day I showed this question to my TA, who's a PhD student in theoretical computer science. After 40 minutes he was also stuck with nothing.

Today I woke up with smelly breath and a eureka. The solution is beautiful and simple.

Now I ask myself: what have I learned from this? Realistically - not much. I am upset that the interviewer didn't ask me the right questions to guide me towards the right train of thought. I don't know why Justin told me "this should work," when my solution in fact didn't . I know that this should have come up in the test cases he asked for, but since I missed the flaw when coming up with the algorithm, I didn't think of testing for it.

I signed the contract with Amazon for next summer and I'm really excited about that! At the same time I can't help but ask 'what if?'


Here's the gist for the solution.

The logic is as follows:

If we traverse the list from left to right, the amount of water at each index is at most the largest value we have discovered so far. That means that if we knew for a fact that there is something larger or equal to it somewhere on the right, we would know exactly how much water we can hold without spilling. Same goes for traversing in the opposite direction: if we know we have found something larger on the left than the largest thing on the right, we can safely fill up water.

With this in mind, one solution would be to first find the absolute maximum value, traverse from the left to the maximum, and then traverse from the right to the maximum. This solution does 2 passes: one to find the maximum, and the other is split into two subtraversals.

The solution in one pass (shown in the gist) avoids finding the maximum value by moving two pointers from the opposite ends of the array towards each other. If the largest value found to the left of the left pointer is smaller than the largest value found to the right of the right pointer, then move the left pointer one index to the right. Otherwise move the right pointer one index to the left. Repeat until the two pointers intersect. (This is a wordy explanation, but the code is really simple)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园建设方案旨在通过融合先进技术,如物联网、大数据、人工智能等,实现校园的智能化管理与服务。政策的推动和技术的成熟为智慧校园的发展提供了基础。该方案强调了数据的重要性,提出通过数据的整合、开放和共享,构建产学研资用联动的服务体系,以促进校园的精细化治理。 智慧校园的核心建设任务包括数据标准体系和应用标准体系的建设,以及信息化安全与等级保护的实施。方案提出了一站式服务大厅和移动校园的概念,通过整合校内外资源,实现资源共享平台和产教融合就业平台的建设。此外,校园大脑的构建是实现智慧校园的关键,它涉及到数据中心化、数据资产化和数据业务化,以数据驱动业务自动化和智能化。 技术应用方面,方案提出了物联网平台、5G网络、人工智能平台等新技术的融合应用,以打造多场景融合的智慧校园大脑。这包括智慧教室、智慧实验室、智慧图书馆、智慧党建等多领域的智能化应用,旨在提升教学、科研、管理和服务的效率和质量。 在实施层面,智慧校园建设需要统筹规划和分步实施,确保项目的可行性和有效性。方案提出了主题梳理、场景梳理和数据梳理的方法,以及现有技术支持和项目分级的考虑,以指导智慧校园的建设。 最后,智慧校园建设的成功依赖于开放、协同和融合的组织建设。通过战略咨询、分步实施、生态建设和短板补充,可以构建符合学校特色的生态链,实现智慧校园的长远发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值