Mo Gawdat: “Solve for Happy: Engineer Your Path to Joy” | Talks at Google

Mo Gawdat is a remarkable thinker and the Chief Business Officer at Google’s [X], an elite team of engineers that comprise Google’s futuristic “dream factory.” Applying his superior skills of logic and problem solving to the issue of happiness, he proposes an algorithm based on an understanding of how the brain takes in and processes joy and sadness. Then he solves for happy.


List at each depth of a binary tree

Problem: Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth (e.g., if you have a tree with depth D, you’ll have D linked lists).

My solution:

public class BinaryNode {

	private BinaryNode left;
	private BinaryNode right;
	private int value;

	 * Populates the argument mapOfLists with a linked list of elements at each
	 * depth. The key of the map will be the depth (starting with 0).
	 * To invoke this on an entire binary tree, call providing the root
	 * BinaryNode and a depth of 0.
	 * @param node BinaryNode to process in the current iteration
	 * @param depth starting with 0
	 * @param mapOfLists see above
	public static void createMapOfOneListPerDepth(BinaryNode node, int depth,
			Map<Integer, LinkedList<BinaryNode>> mapOfLists) {
		if (node == null) {
		if (mapOfLists.get(depth) == null) {
			mapOfLists.put(depth, new LinkedList<BinaryNode>());
		createMapOfOneListPerDepth(node.getLeft(), depth, mapOfLists);
		createMapOfOneListPerDepth(node.getRight(), depth, mapOfLists);

Full code including accompanying JUnit tests can be found at my GitHub.

Java 8 – method references

Remember, lambda expressions are anonymous methods and they remove a lot of ceremonial code around the main instruction that the code is conveying.

Now consider the case where a lambda expression is simply calling a method that already exists, and nothing more. In that case, we can simply things even further.

If, say, you have a class SomeClass that has a method someMethod(int a, int b) then the lambda expression

(a, b) -> SomeClass.someMethod(a, b)

can be written using a method reference as just


Again, don’t confuse unfamiliar with more complicated. With practice you’ll see when lambda expressions and method references get the point of your code across better than the old way of doing things would.

There’s a little bit more to it, though. There are 4 different usages of method references.

Kind Example
Reference to a static method ContainingClass::staticMethodName
Reference to an instance method of a particular object containingObject::instanceMethodName
Reference to an instance method of an arbitrary object of a particular type ContainingType::methodName
Reference to a constructor ClassName::new

If you’d like to see more examples of method references check out the official docs here.

Making Badass Developers – Kathy Sierra keynote

Amazing talk by Kathy Sierra – author of numerous authoritative texts on Java – on “how to get way better, way faster”.

Summary points:

  1. No one has infinite cognitive reserves – even though we are often treated as if we do.
  2. Cognitive reserve can be thought of as a single tank that can be depleted in various ways, leaving less for subsequent activities. Tiny leaks add up.
  3. Help thy fellow developer by being mindful of draining their cognitive resources unnecessarily.
  4. Rethink the common adage “practice makes perfect” as “practice makes permanent” instead. It makes whatever you practiced permanent – even if they were bad habits.
  5. Key message of this talk: to go from novice to expert, what’s needed is a very high quantity of high quality exposures
    • this needs to be applied to sufficiently small chunks of the overall thing being learned
    • a chunk is too big if you cannot master it in three 45-90 mins sessions, aiming for 200-300 exposures.



Java 8 – lambda expressions


Lambda expression were introduced in Java 8.

Here’s a simple example that illustrates how to use lambda expressions, and how they make your code simpler and more expressive (not to mention background optimizations by the compiler).

Consider this code that iterates over a list to print out its elements.

List<Integer> values = Arrays.asList(1,2,3,4);
for (int i = 0; i < values.size(); i++){

What we’re really trying trying to do is print out each element of the array but we’ve had to write a whole bunch of ceremonial code around the that main task of interest to us. This may not bug you much but that’s because it’s familiar. Lambda expressions are (at present) less familiar. But they are simpler. And simpler code, is more readable code. It communicates the essence of what it’s trying to do more readily. Java 8 introduces two new things – internal iteration with forEach(...) on collections and lambda expressions – that can turn the big messy for loop above into something much more to the point.

values.forEach((Integer value) -> System.out.println(value));

or the following, since Java knows than each element in a collection of `Integer`s is going to be an Integer.

values.forEach(value -> System.out.println(value));

Remember how lambda expressions are essentially anonymous methods? In the code above

  • the bit before the -> are the arguments to the method ((Integer value));
  • after the -> comes the body of the method; and
  • the return type is implied based on the method to which this lambda expression is being passed. For example, a forEach(...) call understandably doesn’t need a return type (i.e. void), it just wants to know what to do for each element of the collection instance it’s invoked on.

Based on this great talk by Venkat Subramaniam at SpringOne2GX 2014

Useful jQuery snippets

Manipulating the DOM

To manipulate the DOM by accessing its elements, use the methods listed at Here’s a couple of examples

// set text of element
// set HTML of the element
$("#title").html("<i>New HTML</i>");
// remove the element from the DOM

Dynamically applying CSS

To use jQuery to apply css classes to DOM elements dynamically, first make put the following in a .css file.

.highlight {
background-color: yellow;

Then do this in the .js file to apply the class to a given element with id "someElem"


This will apply the styling of the highlight css class to the element with id "someElem"

Writing unobtrusive JavaScript


Important principle – since all JavaScript referenced by a page is essentially grouped as one giant JavaScript file, we need to be careful with not using the global namespace (to avoid conflicts).

So pick a namespace for your JavaScript code and keep using that.

E.g. instead of doing

var planet = { // went to global namespace
   name: "Earth",
   sequence: 3

Keep planet in a particular namespace (say “MYNAMESPACE”) as below. But first we need to create the namespace first – well a var that we will treat as a namespace. Another example where JavaScript’s malleability allows it to take on features not exactly inherent to it.

var MYNAMESPACE = {}; 

MYNAMESPACE.planet = {
   name: "Earth",
   sequence: 3

However, if you’re declaring variables inside the jQuery ready() function then those vars are going under the jQuery namespace. To illustrate, the following example would print out “undefined” to the console.

$(function() {
   var someString = "I belong in the jQuery namespace";
   console.log(window.someString); // 'undefined'

Code snippets based on the Microsoft Virtual Academy’s great introductory series on JavaScript.