耗子叔ARTS:第五周
Algorithm:
/** * 206. Reverse Linked List * Easy ** 2220 *
* 61 *
* Favorite *
* Share * Reverse a singly linked list. *
* Example: *
* Input: 1->2->3->4->5->NULL * Output: 5->4->3->2->1->NULL * Follow up: *
* A linked list can be reversed either iteratively or recursively. Could you implement both? *
* Accepted * 571,525 * Submissions * 1,053,195 */
JAVA:
public static ListNode reverseList(ListNode head) { ListNode pre = null; ListNode cur = head; ListNode tmp = null; while (cur != null) { tmp = cur.next; cur.next = pre; pre = cur; cur = tmp; } return pre; } public static class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public static void main(String[] args) { ListNode listNode1 = new ListNode(1); ListNode listNode2 = new ListNode(2); ListNode listNode3 = new ListNode(3); ListNode listNode4 = new ListNode(4); ListNode listNode5 = new ListNode(5); listNode1.next = listNode2; listNode2.next = listNode3; listNode3.next = listNode4; listNode4.next = listNode5; listNode5.next = null; System.out.println(reverseList(listNode1).toString()); }
GO:
func ReverseList(head *ListNode) *ListNode { var prev *ListNode for head != nil { head.Next, prev, head = prev, head, head.Next } return prev } type ListNode struct { Val int Next *ListNode }
Review:
James StanierFollow
May 2
Jack Ma, co-founder and executive chairman of Alibaba Group. Credit: World Economic Forum via flickr/CC BY 2.0
996.
That number means 9 a.m. to 9 p.m., six days a week, and is shorthand for the punishing schedule Chinese workers are expected to maintain. A 72-hour workweek with little time for anything else: No family time. No time to meet friends. No hobbies. Not even time to cook proper meals. Once you account for sleeping and commuting, one might wonder how ambitious tech workers fit in the rest of their lives. Is this the price it takes to get ahead in the booming Chinese economy, or is this a symptom of a hustle culture that has gotten way out of hand?
Nobody can doubt that Jack Ma is successful. As a co-founder of Alibaba, one of the world’s largest e-commerce companies, Ma has an estimated net worth of around $40 billion and often makes lists of the world’s most powerful people. However, his recent comments on the company’s WeChat account about 996 working culture—increasingly prevalent in China—have sparked outrage.
“Many companies and many people don’t have the opportunity to work 996,” Ma said. “If you don’t work 996 when you are young, when can you ever work 996? In this world, everyone wants success, wants a nice life, wants to be respected. Let me ask everyone, if you don’t put out more time and energy than others, how can you achieve the success you want?”
Ma isn’t the only notable businessperson advocating for brutally long hours. An email reportedly from JD.com, another Chinese e-commerce company, noted that underperforming employees are those who don’t keep “fighting” to do more work “regardless of performance, position, tenure, personal well-being issues or family reasons.” Youzan, a Hong Kong–listed e-commerce giant, reportedly demanded that employees follow the 996 routine at its end of year gala event. Bai Ya, the company’s CEO, then defended these comments on the grounds that it would expose more people to the company’s culture and help people truly decide whether they want to work there.
A screenshot of the 996.ICU website taken at the time of writing.
A website recently went live at 996.ICU (the site has been down in recent days due to a technical issue), believed to have been created by protesting Chinese tech workers. The website is hosted on GitHub and was created by an anonymous user who joined the site on March 25. The name 996.ICU refers to the phrase “work by 996, sick in Intensive Care Unit,” which succinctly describes how the 72-hour-per-week working culture touted by Chinese tech giants is not a recipe for sustainable success, but for burnout and serious health problems.
At the time of writing, the GitHub repository had more than 240,000 stars (favorites). In comparison, the JavaScript framework React, the most popular way of building web application UIs, had only 126,000 stars. TensorFlow, the popular open-source deep learning framework maintained by Google, had 125,000 stars. The 996.ICU project is believed to be the most-starred project on GitHub today.
The website highlights that Chinese labor laws prohibit more than eight hours of work per day and 44 hours per week in a standard contract, and that it’s illegal to not offer overtime compensation to workers who clock more hours than the legal maximum. The website also claims that although it’s a recent phenomenon that notable companies have made public statements about the existence of 996 culture, 996 has long been practiced in many Chinese companies.
There is further information stored in files of the GitHub repository. More than 500 users have added information to a list of companies that are practicing 996 working hours, along with when it was believed to have begun and links to evidence of these practices. The list currently contains 110 companies and features globally known names such as Huawei, Alibaba, Baidu, and Youzan. Evidence ranges from screenshots of “voluntary struggle” agreements from Huawei to posts from workers on Kanzhun, the Chinese equivalent to Glassdoor.
Controversy has been amplified by reports that the Chinese technology companies under scrutiny are using their web browsers to block the protest website. According to Abacus, the popular instant messaging program WeChat refuses to open links to the website. Browsers such as Tencent’s QQ, Qihoo’s 360, and the native browser of Xiaomi smartphones restrict user access to the site. QQ displays a pop-up message telling users that the protest page is a “malicious site.” The 360 browser blocks the site and displays a message that it “contains illegal information.”
This could suggest that Chinese tech companies are beginning to take the law into their own hands. These companies are not only publicly declaring that their culture involves working hours that Chinese law deems illegal; they’re also deciding to censor websites they consider harmful to their reputation.
In the initial tech startup boom of the early 2000s, many companies embraced a culture of working around the clock to claim first-mover advantage on their competitors. This period gave birth to Chinese startups that are now some of the most valuable in the world, such as Tencent and Alibaba. Given these companies’ astounding growth, many others have since adapted a relentless work culture in the hopes that they can replicate that success.
The BBC profiled Li Zhepeng, a 25-year old who moved to Shanghai with the hopes of jump-starting his career. As a young man from mainland China living in one of the world’s top cities, he expected to experience the nightlife and culture that draws talented professionals to locations like London, New York, and Paris. The reality was starkly different: Li had to commute 90 minutes each way to the outer suburbs, where office space is cheaper, to work 12-hour days, including Sundays, posting descriptions of items for sale on an e-commerce website. He took home ¥3,500 a month, equivalent to $560.
Individual motivation to work 996 is varied. Some people are in desperate need of the money. Some know that getting ahead in their career means dedicating themselves to their job at the expense of their outside lives. And some have no choice but to comply with the culture of the company they work for, lest they lose their jobs.
On the surface, working 996 is about companies capitalizing on as many hours as possible to make progress. It utilizes sheer brute force to beat the competition and capture a market. However, knowledge work like computer programming is unlike mechanical work: It is not a series of repetitive tasks that anyone can do as long as they can stay awake and crank the handle.
Like many other creative pursuits, like mathematics, writing, or designing, a human’s output on a given task depends on many different factors, such as the quality of the working environment, a worker’s exposure to stress, and the ability to rest well and frequently. Sometimes programmers can butt their heads for hours against a problem they cannot solve, only for the solution to come to them the next morning after a good night’s sleep.
Working endlessly on a punishing schedule can make people less effective than if they worked fewer hours in a calmer manner. Tired employees can do sloppy work and introduce bugs that cause downtime and even more effort to fix. Some of the greatest minds in history — those who have produced defining works for humanity — have vouched for shorter workdays to be at their creative peak. While it’s true that Darwin, Poincaré, and Thomas Mann are geniuses with superior intellectual abilities, they confined their creative output to daily blocks of three to four hours and filled the rest of their day with other activities.
The greatest waste of 996 culture is that it’s symbolic overwork that is detrimental to employees’ mental and physical well-being. It does not guarantee that those employees are producing better work than their peers who work saner schedules. It deprives people of their free time and makes families and relationships suffer—for no extra pay and no extra output. It is akin to cultural imprisonment, where workers are either pretending to look busy until it is acceptable to leave the office or literally working themselves to death.
Overwork culture is not new, and it is not a primarily Chinese problem. Japan has long suffered from this issue. Cultural phenomena such as being unable to leave work until one’s boss leaves and regularly clocking more than 80 hours of overtime a month have been reported for decades. In fact, this extreme overwork has caused death in seemingly otherwise healthy individuals. It even has a name: karōshi, which translates to “overwork death.” The first case is attributed to a 29-year-old male in 1969, with the term becoming more widely known in 1978, when multiple individuals died from overwork-related strokes or heart attacks.
The Japanese bubble economy of the 1980s, which brought frantic economic activity, elevated karōshi to national attention with reports of several notable business executives suddenly dying without any previous signs of illness. Given the current Chinese technology boom, it’s no wonder that working 996 can result in a trip to the ICU.
Tokyo rush hour. Credit: Chris 73 via Wikimedia Commons/CC BY-SA 3.0
Some young Chinese workers, however, are refusing to conform to the harmful cultural norm. Instead of suffering in silence, they are beginning to speak up or show their disagreement by finding work elsewhere. Li Zhepeng, the e-commerce worker mentioned earlier, decided to switch to a different job and be up-front about his working conditions. He spoke candidly with his manager to set a more manageable workload and to ensure that he could occasionally leave earlier. She agreed. His colleagues noted that he was their idol for having the bravery to speak up, according to the BBC.
The 996.ICU GitHub repository also links to another list of companies: those that are reported to work a saner nine-to-five, five-days-a-week schedule, known as 955. A majority of the companies listed are of Western origin, such as eBay, Oracle, Intel, and Apple. With the anti-996 movement creating cultural pressure for nonconforming companies to change their ways, and with younger workers casting their vote with their feet, we can only hope those who are employed in 996 workplaces will find the support to challenge the companies that are setting the agenda.
Although Western technology firms are by no means perfect, numerous pro-worker cultural movements have gained a significant voice in Western countries. Instead of sitting around until our bosses leave, we are beginning to celebrate bosses who leave loudly. At the time of writing, Working Nomads lists more than 10,000 highly skilled jobs that can be done remotely and flexibly from a desk anywhere in the world. Technology companies are beginning to realize that a fridge full of Diet Coke and chocolate is less important than being able to work flexible hours. Jason Fried and David Heinemeier Hansson’s book, It Doesn’t Have to Be Crazy at Work, is a number one Amazon bestseller.
Work should support and enable lives, rather than claim them. Conditions in Chinese technology companies, as they have in other countries, will eventually change, because the current practices are simply unsustainable. In August 2018, two Chinese technology founders died under circumstances believed to be related to high-pressure working environments. But regardless of the bad press, has 996 culture spooked Silicon Valley?
Commentary from angel investor Jason Calacanis.
Many see 996 culture as a threat to Western economies, but we shouldn’t cave in to these practices. Instead, we should focus on strategy and workforce efficiency. Companies should strive to create the conditions that allow their employees and products to succeed without being shackled to their desks. We should clear out meaningless meetings. We should allow for deep, focused work that moves the needle. We should give employees the flexibility they need to be their most productive.
We should work hard, but most importantly, we should go home. That’s how we win the marathon.
Tip:
看linkedlist 源码 看到
if (index < (size >> 1)) {
后来才明白这个移位符是为了二分查找
数字&1 &0 验证奇偶性
Share:
https://mp.weixin.qq.com/s/J_8iMo-M4plvLo1zzw8d2w
本文转载自公众号 大飞码字
一个程序员在没有成长成为架构师之前,几乎都要跟 Bug为伴,程序员有很多时间都是花在了查找各种 Bug上。
我印象深刻的一个Bug, 是一个服务器网络框架无锁队列的 Bug 。那个 Bug 连续查找了五天的时间,才最后定位出来。
当时我们的分布式存储系统出现了性能瓶颈,定位后发现瓶颈是在服务器网络框架上,所以我们决定为此替换一个最新研发的网络框架。这个新的网络框架为了追求高的性能,采用了无锁队列的设计。
第一天编码测试完成后。测试环境跑,完全正常,特地搞了一堆 log 来重放请求,程序跑得特别欢快。解决了当时的性能瓶颈,感觉特别的开心,但好景不长,服务部署到现网环境,跑不到一个小时,就 coredump 了。
尝试上线了几次,每次都是跑半个多小时,就 coredump 。
当时的第一反应是新网络框架的问题,这是很直觉的反应,但很快就产生了怀疑,因为这个框架也有其它模块在用,也没产生问题,当时觉得诡异了。
仔细查看了代码的修改记录,特定检查了版本管理系统的 log, 做了代码 diff, 确定确实只有这部分的修改。凭着经验,我说服自己,这个时候应该坚信最明确的逻辑,不要走到其他歪路上去。
第二天,我把这个框架单独拎了出来,特地写了一个测试的模块,并用测试代码生成了一堆的请求数据,发送给测试模块。它疯狂的运转起来,跑了一个多小时,跑得很欢快,一切正常,啥 Bug 都没有。
开大了并发,继续压,依然没有问题。懵逼了!不知咋回事。再次小心得灰度系统上线,跑不到一个小时还是 coredump 了, 这个时候,我开始怀疑人生了,这个是咋回事了,都想砸键盘了都。
然后我冷静了下来,经验告诉我,这时应该按照正常流程完整地跑一遍测试模块。于是我把那个测试模块打包成了现网模块,切走了一台现网机器的流量,把测试模块给上线到了一台现网机器。
之后用工具往现网机器发送数据,不到一个小时,coredump 了。终于复现了这个 cordedump, 那一刻犹如哥伦布发现了新大陆,简直欣喜若狂啊。这个时候已经是第三天了。
我复现了 Bug,但依然没办法定位出具体的原因。coredump 出来的栈是全乱的,没有任何价值,接下来,就开始用 log 跟踪法了。
我依据数据的流转过程,在每个关键点,都打上 log,log 包含了所在的函数,行数,程序逻辑的编号,全部的关键数据等等。我仔细地设计了这个log,争取打得不多不少,太多容易看晕头,而且太多无效的信息,会掩盖了真正的问题;太少,信息不足,又不足以判断,所以这种情况下打 log 也是个艺术活。
通过精心设计的 log, 终于发现数据在一个特定的环节混乱后,程序就一定会 coredump。分析log , 发现数据包在最后时刻是完整的,但包似乎出现了乱序和重复。这个时候,才开始意识到可能是无锁队列的问题,因为只有队列出问题,包的进出顺序才会乱掉。
然后又花了半天的时间,专门为无锁队列写了测试用例,用数据疯狂地怼。在测试环境,依然一切安好,但上线到正式环境,压测半小后,终于挂了。终于看到了胜利的曙光!这个时候已经是第四天了!
到现在已经很明确是无锁队列的问题了。但这个数据结构的代码不到 200行。我拉了两个同事一起 review,都没发现问题。但就是 coredump 了。
奇葩了,又陷入了人生怀疑,开始怀疑内存,怀疑 cpu,结果换了机器,还是一样。
后来,仔细地对比了现网环境和测试环境的区别,机器类型,操作系统版本都一样。然后编译器?咦!编译器?上去看了一下,结果发现编译器的版本不一样!这段时间我所使用的现网编译环境升级了新的 gcc 版本,但测试编译环境,还是旧的版本的。(这个也比较坑)
当时的直觉是肯定跟编译器相关,但代码都一样,难道是编译器bug?不可能吧 ?! 后来想,不如将它们转换成汇编看看吧。于是用两个版本的编译器将C的代码各自转换成了汇编。然后diff 汇编代码,哇!发现真的有一行是不同的!
后来自己分析对比,发现是因为我们开启了gcc最高级别的代码性能优化,不同版本的 gcc 在一些没有特定依赖的语句上的优化是不同的。
说人话,就是有一段代码,如果加了锁,两个版本的编译器下,都会产生一样的汇编,如果没有加锁,代码有一行的顺序被调整了,当然,从编译器优化的角度讲是对的,是我们使用姿势不对。
但无论怎么样,终于找出了这个问题。苍天啊! 找了五天呀! 最后当然是开开心心地上线了。
查这个bug 确实花费了很多的时间,不过也是没办法,你不解决bug,就不能上线,但中间也收获不少,特别是对编译器优化有了很深的印象,也算是为自己的查 Bug 能力,又贡献了一波经验吧。
对于 Bug , 我分享下自己的一些认识和建议。
面对 Bug 的态度
只要你持续地写代码,就一定会持续的产生 Bug,所以第一个事情是要摆正对 Bug 的心态。我遇到过两个极端。
第一个极端。
遇到过一个leader,对系统质量相当重视,对我们写的代码要求很高,每次设计并写完一个新的系统,他喜欢跟你算这次的系统上线,产生了多少次故障,这半年时间产生了多少个 Bug,每个 Bug 的影响范围如何。
我们一堆人被搞得特别累,战战兢兢,到后面,大家都比较排斥去做优化,去重构代码,只求无过,不求有功了。所以,我觉得这种方式不好,对 Bug 带着一种比较包容的态度去看待,可以减少不少的心理负担。
第二个极端。
后来去了一个新的团队。新的团队很重视业务和工程迭代的速度,所以对代码质量和 Bug 的容忍度很高。如果是一个新上线的业务,是默许 Bug 存在的。
这种对质量过于松散的要求,在后面也带来不好的影响。大家对 Bug 太免疫了,以至于出现 Bug 和 故障的时候,大家都不够紧张。系统质量有一段时间出现比较大的问题,还因此被部门经理特训了一番,后面通过各种措施,才慢慢提高了整体的系统质量。
上面两种极端都不可取,应该很重视 Bug,尽量避免 Bug,但也不应该唯 Bug 多少论业绩。
具体到 Bug 的查找上,我说说我的一些经验。
Bug 的复现
我把 Bug 分为可重现的 Bug 和不可复现的 Bug 。
对于可重现的 Bug , 查找起来比较容易,比如可以用”二分查找“的方式,从模块层面开始定位起,每次折半,每次折半地缩小范围,一直到代码层面。
在代码层面,遵循一些常用的原则:比如看到内存拷贝,直觉上要想到内存越界;看到数组,就要考虑是否索引越界;看到指针,就要考虑是否正确解构;看到多线程,就要考虑是否线程安全;
对于不可重现的 Bug , 第一步就是要把它重现出来。有时候特别的难,特别是并发形态下产生的 Bug ,出现的时机和触发条件都不清楚。对于这种 Bug ,只能通过各种尝试去复现它。
比如将多并发调整为单并发的方式,看能否复现,如果可以复现,就可以转化为可复现的 Bug ,用”二分查找“的方式去排查。
如果不能复现,那极大概率是并发问题。这个时候最好先停止排查,仔细分析程序在并发状态下可能出问题的点。大部分并发问题的根源,是互斥数据没有被正确读写,或者一些共享状态被错误修改。
静态代码检查
利用coverity等代码检查工具进行代码的静态检查可以发现很多潜在的问题,而且修复的成本很低。团队后来引入了这个检查工具,确实带来了不错的效果。类似变量未初始化,疑似的内存越界等都有可能被检查出来。
编译器的 warning。有些同学一开始的时候对 warning 不重视。我们团队早期也遇到过这个情况。
那时候产品迭代的速度很快,所以大家写完代码,能够编译通过,就进行各种测试,然后准备上线了。一开始的一两个warning, 不太理会。后面发现越积越多,到最后终于成为一个不得不解决的问题。
部门还为此特地立项,来消除warning。先是在内部多次强调了这个理念,然后从基础库,基础模块开始实施,基础代码部分统一 fix warning, 然后开启编译器把 warning 当 error 的开关。完成之后,再逐步地推业务模块进行修改。反正折腾了好一段时间。
工欲善其事必先利其器
代码 Bug 出现的时候,善用一些排查工具可以极大得提升效率。比如对于C/C++ 的 gdb 调试,内存泄漏时候 valgrind 的检测,linux 下面用 perf top 来析 cpu 的消耗等。
一开始的时候,我对这些工具不重视,老是觉得真正使用的时候,去查文档就行。后面才发现,用工具查着问题时候,遇到不会用的命令或功能,再去查文档,是个痛苦的事情,来回切换的开销也使得效率低下。
后面就对这些辅助工具的使用重视了起来。,专门花时间去学习和练习使用,反而提升了不少的效率
打 log 的艺术
很多时候,出现一个 Bug,未能定位出来,需要打上更多额外的 log 来辅助排查。一开始的时候,是想到一点,打一个 log,后面发现这么做没有章法,逻辑不清晰, 排查效率低下。
后来学会了,遇到 Bug 后,先在脑子里面分析一番,然后花一两个小时详细地设计 log 的格式和打 log 的位置。发现这种方式对查问题的效率提升很大。
所以遇到 Bug 的时候不要急躁,先静下来心来分析,在脑海里尽力重现出完整的运行逻辑,然后仔细地进行 log 设计,包括 log 包含的字段,打 log 的点等。这样能极大的提升排查问题的效率。
结语
Bug 是程序员最不愿意用面对,但又经常出现的一个 “诡异生物“。对待Bug 要有正确的心态,最好不要跟业绩强绑定,也不能太过于疏忽。Bug 的排查是个很复杂的事情,每个人都有自己的方式和做法,如果你有好的做法和建议,也欢迎在留言区分享给大家!