Search This Blog

Thursday, November 22, 2018

2018年10月 玛氏面试回顾

玛氏呢,我想大部分对快消没兴趣的人是没听说过这个公司的,不过对于喜欢快消的人,这个公司(或者说至少它的工作岗位)是跟宝洁、联合利华并列成为 “一线” 的。联合利华(以及雀巢)的校招比较混乱,部门太多、结构不清晰,而且产品数量有限(大部分人应该是不想去食品和餐饮部门的),无形中身价降低,于是只剩下玛氏跟宝洁,可以称之为真正的 快消求职 “一线公司”。

我很早就知道玛氏了。3-5月份曾经想申请它的暑期实习,结果硬是被我拖延过了递交的截止日期。

在网上可以了解到,玛氏的GMT综合管理培训生,工资属于快消管培界最高,跟百威的GMT都属于工资远高于其他公司的岗位。不过玛氏也有其他部门,销售、供应制造、IT、研发等,这些相对来说就没那么强调所谓“领导力”(当然面试的时候还是会问),也没有那么热门了,据说工资也远不如GMT有吸引力。

每个人可以同时填报GMT和另一个部门,大部分人GMT都被刷掉了,进入了其他部门的筛选环节。

玛氏筛选分为笔试(考察数字推理、英文阅读、听力)、口语(托福口语形式)、群面、第一次单面,以及最后的一整天AC。每个环节都有人被刷掉。

群面比较常规,小组六个人每人读一份材料,六份材料拼起来是一个大的案例,每人依据自己的材料扮演整个案例里的某一个角色。我们小组呢,另外五个人战斗力比较低,或者可能由于面试经历不足吧,没有意识到这个讨论环节其实是需要大家各自去争取自己的角色所需要的资源的,因为虽然每个角色都有自己所要完成的任务或目的,但由于整体案例的资源所限,不可能每个人的每个requirement都达标。结果讨论时我一明确提出我的角色所要求的资源,小组完全听从了我的要求,直接自愿牺牲自己的目的,最后做出个方案,还超出了预算,负责finance角色的同学竟然没有反对意见,还得我跳出来说绝不可以超过budget…

群面最后一个环节是每个同学做一个简短的自我批评,说说自己哪里做的不够好,没想到另外五个人(可能是由于第一个人这样子开头,她们就效方了)全都在检讨自己讨论的时候不够认真、没有倾听、只顾自己说… 哪能这样子反思啊,太老实人了呀!一定要讲一些无关痛痒的自我批评,比如某个点由于时间限制我没有很细致沉入地思考,然后强调一下,虽然犯了一点小的失误,但并没有影响到整体结果。

群面完六个人一起去高德置地到处乱逛。六个人里,有一个本科广外,研究生凯斯西储的女生,有一个澳门大学的研究生男生,一个中大管院的本科女生。跟管院的女生聊了挺多,她拿到了安永的审计,说她投了所有四大,但只有安永投的是审计,也只有安永拿了offer。还投了宝洁的FA,没进面试。没打算申请出国,一心找工作。只有我接到了下一轮面试的电话,下午五点十分。

单面两个中国面试官,但是全程英文。

单面两个面试官,就是群面时观察我们小组的两个人。这个单面完全与我的预期不同。之前准备和参加了宝洁的面试,所以我对单面的预期都是要我举例证明我的某个能力,脑子里全是过往经历,结果自我介绍完,面试官上来就要我评价之前群面的小组表现和我的个人表现,谁是小组的领导者(我说我起到了subtle leadership),你作为领导者是不是应该为小组表现(不好)负责,等等。我一直坚决不肯承认我(以及整个小组)在群面中的不足,无论面试官丢来什么问题challenge我,我都绕着圈子夸整个小组的每个同学以及我自己,不说任何人的坏话,最后面试官发现也问不出来什么所以然,都无奈地笑了。

接下来更是直接开始脱离案例,问我的想法,比如“你如何看待领导力?”,“你如何解决团队中的冲突?” 等等,我没有准备这种提问的方式,所以全是胡扯回答。

当天(周二)晚上九点收到邮件告诉我通过,周四去参加一整天的AC面,看了一下群里的人数,IT部门总共二十几人进入了AC。因为跟宝洁的录取庆祝会冲突,最后决定去参加celebration,就没来玛氏的AC面了。




玛氏的IT中心在7楼,窗外能看到美国领事馆


跟管院的女生闲逛的时候,一抬头第一次看到了GE广州,很激动

面完以后在珠江新城闲逛。面试官说六点半之前能出结果,我就一直等到六点多才打车走,结果其实晚上快十点才发邮件

2018年10月 宝洁面试回顾 (一)

在所有在这个秋季至少尝试了找工作的同学中,我应该算是面试数量中等的吧。一方面我确实投了很多公司,参加了很多面试,相比那些不是很全心投入的同学而言,肯定算是有一些经验了;另一方面,我又有一个很严格的list,凡是没有被我列在我的list上的公司(而我的list也不会持续增加新的公司),我连投都没投,所以相比那些海投的同学,可能我的数量还没有那么夸张,不过据我在群里看她们聊天感觉,海投的人往往只是收到了一大堆笔试,而由于她们的心态/策略/时间安排上的问题,准备不足,往往连笔试她们都过不了,所以具体参加了多少面试其实也不得而知,可能也没有很多… Anyway,让我们回顾一下我的宝洁面试经历吧,或者说我的整个申请宝洁的全部流程和思路。

有一点要说明的是,虽然这些面经会按照公司分成不同的系列,而每个系列又会有(一)、(二)这样子,但这绝不是一面和二面的意思,所有关于面试的具体经历会放在一篇文章中,分P往往是为了划分主题,比如在本篇文章中,我不会讲我的面试经过、一面二面等等,而是generally(从总体上)讲讲我对申请宝洁的想法和观点,不一定对。

说起来我对宝洁的执念源于一个很微小的场合,或许这也是所有的影响一生经历的事情的发生方式吧,很多事情你往很久很久以前去追溯,看清事情中的因果关系,会发现往往就是当时随口说的一句话,或者类似的东西,改变了后来的很多事情。

当时是在无印良品兼职,去年12月初左右吧,上完一个晚班,跟刘颖娴一起等地铁的时候,随口一说我以后想去宝洁,其实当时我也确实是觉得宝洁很不错的了,不过一方面相信以自己的社团经历(当时我以为所有公司的面试,除了技术岗,一律是问你的社团经历,大家都是学生会主席啥的… 可见当时我有多么的幼稚,以及这一年来我改变了多么多)是根本不可能通过面试,另一方面是当时其实对未来的规划还是出国读研的,甚至连专业当时都觉得会继续学CS。然后她就问我我想去哪个部门,其实我对部门没有概念的,直到今年真正申请时,我都对部门完全无所谓,我找工作看公司看行业,不太看岗位具体做什么(除非是技术岗这种我确实干不了的),但为了避免好像我完全不懂的尴尬,我就随口说了销售。

后来一月份的时候,当时就打算找实习,申了杨森的一个助理岗,完全懵逼的状态就去面试了,还特地跑去买了一双休闲风格的皮鞋,但当时是真的没有任何面试经验,而且一共两个人面试,另外那个人是个超级帅哥,当时的我还特别没有气质,自我介绍也是乱七八糟,简历还用的是当时申请夏令营时候的英文简历,内容也乱七八糟,面试官问我未来职业规划的时候我回答不知道… 总之二十多号的一个周一面试,面完就感觉肯定挂了。

当时还想申请GE医疗的一个图像处理实习岗位,专业性很高,我还认认真真去广州图书馆学习了好几天关于医学检测试剂和技术之类的东西,结果在大街网上把简历投出去,也没有收到任何回复。

到三月份的时候就开始准备找实习了,说实话如果不是出于纯粹的狗屎运而去上了辉瑞的话,我怀疑我当时的经历根本不会被任何一个我看得上的公司录取实习。不过anyway我是去上了辉瑞,有了个算是正经的实习经历。后来又去了尼尔森。

快进到十月份。宝洁申请9月9号开始,10月10号结束。我是一直拖到了十月8号才提交了网申(其实就是电话和邮箱),收到了网上测试链接,而一直到了10号晚上才去做题。

其实当时我是打算放弃的(请literally理解这句话)。之前毕马威的面试经历和等待经历给我带来了一定程度上的心理阴影,所以我觉得真的没有必要去再一次折磨自己了,于是就一直拖着,导致的就是我做宝洁的图形测试(据说网上全有原题)完全是临场发挥没有准备。

总之,10号晚上的大约十点半,经过了一系列不是很激烈的思想斗争以后,我决定放弃宝洁的校招了,上床准备玩手机睡觉。刚上床躺了不到两分钟,就想起我之前去普华永道Stem Day面试的经历,同时也是想起了自己在辉瑞学到的最重要的一课(literally 最重要),任何事情尝试一下最大概率不会比完全不尝试差。

于是大约十点四十分,我从床上跳下来,头脑混乱地点击了邮件里的链接,开始做题。图形题说实话,真的很难,我完全是找各行各列里面的各种形状的count,然后试图发现数字之间的规律。我相信这个思路是不对的,所以做完就觉得肯定挂在这上面了,然后就去做性格测试,性格测试这里,说实话,倾向性和诱导性还是比较明显的,我相信即使你是个社交能力和领导力为零的笨蛋,你也大概知道该选哪些选项才是宝洁想要的人,但不知道为什么这上面莫名就挂了很多人,也是奇怪。

做完之后,大约十一点四十分收到了上传简历的邮件,知道自己笔试通过了还是有点意外的,然后就想,自己也没有为宝洁准备简历,之前的简历上的事例都是为了表现自己是个刻苦工作的听话乖孩子,根本没有提到领导力,所以这里又一次有了一点点想要放弃的想法,不过发现之前毕马威的简历,虽然事例里没有领导力,但排版还算清晰整洁,就用它了。

这里同时还要选择部门。说实话,通过了笔试以后,我还是挺希望能有个面试机会的,所以当机立断决定选择了IT和PS。没错,是在这个last moment我才决定的选择什么部门。

十一点五十左右上传好了简历,后来听说一过零点就不能上传了,万幸。

12号收到一面邀请,手机打来的,当时忘了因为什么原因心情不太好,所以我以为是个骚扰电话,接起来就没说话,等了一会儿对方说话才知道是宝洁面试,尴尬… 对方是Tony,我想既然负责打电话邀请面试,那应该不是什么很高的职位,小职员一个吧,就没有很有压力了,结果是前几天offer celebration上才知道,他是IT总监…

可以选15到26号,想好好准备一下,那就往后选吧,选了25号。后来看面经有点后悔了,感觉大家二面都结束了我才去一面,基本不可能过了。所以你看,宝洁对大家绝对公平,不会说你面试晚你就没机会了。

事实是期间基本没有准备,面试头一天晚上才开始看面试题写自我介绍。

宝洁面试呢,网上盛传,就是八大问,然后大家一直说,它会很深入的追问你的例子的细节,比如数字呀什么的,看看你是不是在胡编。

这基本是事实,但也概括地有些偏。所有公司的面试,大体上分两种问题吧,一种是问你的事例,也就是任何事情都是要你举出以前你的事例,另一种就是问你对某个事情或者某个能力的看法,不用你举出事例。宝洁比较极端,全部都要事例,整个面试,完全围绕你的过去经历。而大家又知道说宝洁最强调领导力,那我怎么体现领导力呢,或许当过社团/学生会/班级的主席/干部/班长 等等会是不错的吧?

这样想你就完全错了。不是说你不要讲你做了什么什么的主席,而是说,你想想,全国几千所大学,里面有几万个学院,每个学院都有学生会,每个大学也都有,每个社团都有主席,每个班级都有班长,这些东西都是by nature必然存在的职位,其本身没有任何意义,你做了这个职位,这个fact本身并不能说明任何事情,因为不是你做,也总会有其他人做,因为这个职位它不可能没有人。当然你做了这个职位是好的,因为它enable了你去实现一些可以发挥你的领导力的事情,可是你具体有没有真正在这个职位上做出了这样的事情,这才是最关键,不要被官职一叶障目,宝洁要的是例子,用Rene Co总裁的话说,就是“代表作”。

说到例子,就要讲到为什么大家都说宝洁对事例中的数字等细节挖得很细。因为大家的例子都太平庸了,我做了什么学生会/社团的主席/部长,组织了什么活动,等等,这些例子,如果本身不是非常有特色的活动的话,其实真的没有什么亮点的,而且面试官还不知道你讲的到底是真的还是假的,且不说这个活动究竟足不足够展现能力,且不说你究竟是不是整个活动的领导人,甚至连这个活动有没有真实发生,面试官都不知道的,所以只能靠多问细节。如果你的例子如此特殊,以至于面试官面试了这么多年,还是头一次听到(其实这才是正常的情况吧?),她基本不会追问细节了,只会听你讲。因为她不怀疑你讲的东西的真实性。

八大问里面,领导力必问,这个完全不存在侥幸,绝不可能不问的这个。所以事先准备一个或多个领导力的例子。制定并实现高目标,基本也会问,这里要注意,你的目标要是确实高,也就是说,其他人没有制定这样的目标,才可以。创意(creative/innovative)这个基本也会问,也要事先准备例子。另外五个问题可以随缘,不一定会问,当然多想几个事例总是好的。

这里介绍一个我自己的经验,估计会对大家比较有用~ 是我面试前一天晚上绝望地准备的时候摸索出来的。

宝洁面试,除了自我介绍(应该没有人不事先背好自我介绍的吧?)以外,确实是只有这八大问,这个基本准确,但是呢,八大问提问的顺序却是不一定的,有可能第一个问题问你领导力,第二个问创意,也可能第一个问你高目标,第二个问创意,最后才问领导力。

不过像我这种经历很少,没有很多漂亮事例可讲的人,有可能你拿出一个事例,回答了一个不重要的题,结果最重要的题就没有事例可讲,或者人家面试官第一个问题是不重要的,结果你就把一个不是很出彩的事例拿出来讲,以为可以把出彩的事例留到后面当杀手锏,结果其实人家听了你这第一个例子就直接失去兴趣了… 

大家要记得田忌赛马这个故事。准备面试的时候呢,第一步是想,我的经历里有哪些最出彩的事例,这里先不要管它体现了你什么能力,先把事例列出来,然后分别看,每个事例里体现了八大问的哪个能力(对应哪个题目),这里要记得,八大问里最重要的就是上面说的那三个,领导力、创意、高目标,所以记得把最出彩的事例(宝洁叫“代表作”)优先留给这几个问题。

同时也要事先计划好临场该用哪个。事先就计划好,如果她第一个问题问领导力,我讲了一个以后,后面创意我用哪个例子?而如果她一直拖着,不问领导力(最后问),那我怎么安排事例,才能既不浪费好的例子,又不在前面几个问题上失分太多?而如果她对我的某个事例感觉不满意,要我再举一个(甚至更多),我要不要一股脑把准备了的全讲出来?

我二面的时候基本就是这个策略,中间出了一点小的意外就是,面试官对我的领导力事例虽然满意,但感觉太过短暂,所以一共要我讲了三个领导力事例。

写到这里我感觉好像整个文章很乱,大家看得肯定是很烦,所以我最后说一点我的想法… 别打我。

整个申请过程(包括面试),一面之前的晚上,二面之前的晚上,包括笔试的那个晚上,我都认认真真地考虑放弃(请literally理解这句话),一面是因为当时真的觉得宝洁离我实在太遥远了,我实在太菜了,面试会把我问得无地自容,更加感觉自己一无是处,所以很不想去刺激自己,结果一面完不到二十分钟就告诉我通过了。

二面呢,我看网上面经说一面都不会深挖细节,二面会抠得非常细,问到你无话可说,我一想,我还是不要去受这个刺激了吧,像我这样子根本没有领导力的人,侥幸通过一面已经不容易了,二面还有外国人,我是不是就不要去了… 没想到二面当场通过。

所以呀。去试试。

主席怎么教导我们的来着?

那么人呐就都不知道,自己就不可以预料。一个人的命运啊,当然要靠自我奋斗,但是也要考虑到历史的行程。我绝对不知道,我作为一个上海市委书记怎么把我选到北京去了,所以邓小平同志跟我讲话,说“中央都决定啦,你来当总书记”,我说另请高明吧。我实在我也不是谦虚,我一个上海市委书记怎么到北京来了呢?但是呢,小平同志讲“大家已经研究决定了”,所以后来我就念了两首诗(原话如此),叫“苟利国家生死以,岂因祸福避趋之”,那么所以我就到了北京。


二面之前的那个晚上。二面是早上十一点,而我凌晨两点半了,还在准备。

第一次看到宝洁的标,还是挺震撼的…

坐在沙发上等助理来接我,可以想象我有多慌坐在那里

等待面试(一面)


Sunday, October 15, 2017

LeetCode Blog for course "Algorithms" -- Problem 11

Problem 11. Container With Most Water

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.


My solution in Python:


class Solution(object):
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        left, right = 0, len(height) - 1
        ans = 0
        while left < right:
            if height[left] < height[right]:
                area = height[left] * (right - left)
                left += 1
            else:
                area = height[right] * (right - left)
                right -= 1
            ans = max(ans, area) 
        return ans


Review:

This problem belongs to the more simple ones. The trick here is that we don't really need to examine all combinations of two vertical lines, we only need to discard the shorter one. The height used in calculating area is the shorter one of the two lines, so it does no good to retain the shorter one after computing the current area. We move on and retain the longer line, until the two lines meet.

LeetCode Blog for course "Algorithms" -- Problem 9

Problem 9. Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.

My solution in Python:


class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if x < 0:
            return False
        copy, reverse = x, 0

        while copy:
            reverse *= 10
            reverse += copy % 10
            copy /= 10

        return x == reverse


Review:

After much thought, I cannot solve the problem without using extra space, so my solution does use some extra space as a new integer is constructed.
Negative numbers cannot be palindromic due to the preceding "-" sign which doesn't exist at the end, so we first check whether the given number is less than 0.
If it is greater than or equals 0, we construct a reversed version of this integer, then compare them. If the original integer equals to its reversed version, then the number is palindromic.

LeetCode Blog for course "Algorithms" -- Problem 8

Problem 8. String to Integer (atoi)

Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (i.e. no given input specs). You are responsible to gather all the input requirements up front.
Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

My solution in Python:


class Solution(object):
    def myAtoi(self, str):
        """
        :type str: str
        :rtype: int
        """
        str = str.strip()
        if str == "" :
            return 0
        i = 0
        sign = 1
        ret = 0
        length = len(str)
        MaxInt = (1 << 31) - 1
        if str[i] == '+':
            i += 1
        elif str[i] == '-' :
            i += 1
            sign = -1
        
        for i in range(i, length) :
            if str[i] < '0' or str[i] > '9' :
                break
            ret = ret * 10 + int(str[i])
            if ret > sys.maxint:
                break
        ret *= sign
        if ret >= MaxInt:
            return MaxInt
        if ret < MaxInt * -1 :
            return MaxInt * - 1 - 1 
        return ret


Review:

According to the requirements in the description, as long as the string starts with number digits (ignoring whitespaces), that substring of number digits is converted to an integer. So first we use the "strip" function to remove all consecutive whitespaces from the beginning and end of the original string. Then we check if there is a sign character ("+" or "-") at the start of the string. Then we read the string one character by one character until a non-number character or the end of the string is reached. We check if the converted integer exceeds the limits of integer. The algorithm ends here.

LeetCode Blog for course "Algorithms" -- Problem 7

Problem 7. Reverse Integer

Reverse digits of an integer.
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? i.e. cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.

My solution in Python:


class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        if x == 0:
            return 0
            
        neg = 1
        if x < 0:
            neg, x = -1, -x
        
        reverse = 0
        while x > 0:
            reverse = reverse * 10 + x % 10
            x = x / 10
        
        reverse = reverse * neg
        if reverse < -(1 << 31) or reverse > (1 << 31) - 1:
            return 0
        return reverse


Review:

This problem belongs to the more simple questions. However, we do need to consider the special conditions, such as numbers that exceeds the limit of 32 bits when reversed. Therefore, after reversing all the digits, we check to see whether it exceeds -(1<<31) or (1>>31), and if it does, we set the reversed value to 0.

LeetCode Blog for course "Algorithms" -- Problem 6

Problem 6. Zigzag Conversion


Write the code that will take a string and make this conversion given a number of rows.

My solution in Python:



class Solution(object):
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if numRows==1: return s
        tmp=['' for i in range(numRows)]
        index=-1; step=1
        for i in range(len(s)):
            index+=step
            if index==numRows:
                index-=2; step=-1
            elif index==-1:
                index=1; step=1
            tmp[index]+=s[i]
        return ''.join(tmp)

Review:

This problem asks us to convert a given string to a zigzag form, giving the number of rows the zigzag shape should contain of. It does not require the exact shape to be drawn, only the result string combining the rows of the zigzag is required. Thus, this problem is basically asks for a new arrangement of the original string.
First we construct a certain number of strings, the number of strings equals to the number of rows in the zigzag. Then for each character in the original string, we decide which of the substrings should it goes to (be appended to). The decision is made using the following procedure.
First we put the first character in the original string in the first substring. Then for each following letter, we put it in the next substring (the next row in the zigzag). This is achieved by setting the "step" variable (increment) to 1.
When the last row of the zigzag is reached, after putting the corresponding letter in that row, we alter the value of "step" to -1. Thus in each following iteration, rather than going down, we going up along the zigzag, putting the next letter one row above the previous one. "Step" is again set to 1 when the first row of the zigzag is reached.
The algorithm ends when the last letter in the original string is processed this way.

Wednesday, September 27, 2017

LeetCode Blog for course "Algorithms" -- Problem 3 & 5

Problem 3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.

Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.


My solution in Python:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        answer = 0;
        left = 0;
        last = {};
        for i in range(len(s)):
            if s[i] in last and last[s[i]] >= left:
                left = last[s[i]] +1;
            last[s[i]] = i;
            answer = max(answer, i - left + 1);
        return answer;

Review:
We make good use of the "dictionary" in Python here. Starting with the first character in the string, we go through the string characters one by one, and store one item in the dictionary, with the character being the key, and the place it appears in the string being the value. When we encounter a character that has already appeared previously, we immediately know because this particular key (the character) is already in the dictionary. Thus we move the starting character of the substring to the next position of which the repeated character first appeared in the original string. We then compare the length of the current non-repeated-character-substring with the longest substring we already know. When "i" reaches the last character of the original string, the "answer" should be the length of the longest substring without repeated character.


Problem 5. Longest Palindromic Substring

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example 1:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.



Example 2:
Input: "cbbd"
Output: "bb"


My solution in Python: 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        ansl, ansr, maxx = 0, 0, 0
        length = len(s)
        for i in range(1, length * 2):
            if i & 1 :
                left = i / 2
                right = left
            else :
                left = i / 2 - 1
                right = left + 1
            while (left >= 0) and (right < length) and (s[left] == s[right]):
                left -= 1
                right += 1
            left += 1
            right -= 1
            if right - left > maxx:
                maxx = right - left
                ansl = left
                ansr = right
        return s[ansl: ansr + 1]


Review:
We observe that a palindrome mirrors around its center. Therefore, a palindrome can be expanded from its center, and there are only 2n-1 such centers. By treating odd "i" and even "i" differently, we are able to examine both palindromes with even length and with odd length. This question is actually very interesting and has many (at least 5) different solutions, each with different time and space efficiency. The most advanced one, the Manacher's algorithm, can be found here.

LeetCode Blog for course "Algorithms" -- Problem 1 & 2

Problem 1. Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

My solution in Python:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution(object):
    def twoSum(self,nums,target):
        first=0
        second=0
        for x in range(0,len(nums)):
            for y in range(x+1,len(nums)):
                if nums[x]+nums[y]==target:
                    first=x
                    second=y
                    return [first,second]

Review:
We use nested iterations here. For each number in the array, we go through the numbers in the array after it to see if there is a required match. Because there is only one such pair that suits the requirement (add up to a specific target), once we find such a match, we can stop the iteration here.


Problem 2. Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

My solution in Python:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        answer = ListNode(0);
        pointer = answer;
        carry = 0;
        while True:
            if l1 != None:
                carry += l1.val;
                l1 = l1.next;
            if l2 != None:
                carry += l2.val;
                l2 = l2.next;
            pointer.val = carry % 10;
            carry /= 10;
            if l1 != None or l2 != None or carry != 0:
                pointer.next = ListNode(0);
                pointer = pointer.next;
            else:
                break;
        return answer;

Review:
Given that the digits are stored in reverse order in the list, the first node in the list is the least significant bit in the integer, so we can add the two lists directly from the first node to the last node. The solution is very straight-forward. Note that along with checking whether both l1 and l2 have reached their ends, we also must check whether the carry bit equals 0. A non-zero carry bit must be carried to the next iteration and have a new node in the answer list to store it. When both l1 and l2 have reached their end, and the carry bit is 0, the algorithm ends.

Saturday, August 12, 2017

2017年8月12日 在以色列海法

今天一整天宅在宿舍. 基本没干啥.

晚上出去到ATM上取了700块钱.

今天没写作业.

明天去耶路撒冷. 要早起. 明早五点钟就要在宿舍楼下集合.




安息日的学校. 基本上是空无一人.