You are on page 1of 14

# Hacking a Google Interview Programming Interviews Exposed

## Sunday, January 17, 2010

Goals

Demystify the interview process Go over common themes and problems Talk about personal experiences and answer any questions

My Background

## Cycles in Circular Array Problem

Asked at recent Google interview Great problem because it allows interviewer to observe:

thinking process pseudocode writing skills discuss time (Big-O) and memory complexities discuss unit tests discuss real life conditions and their impact on solution

## Cycles in Circular Array Problem

1 2 2 -1

Write a function that returns true when a cycle of length n exists; false otherwise

take n hops, if have visited each element once, return true; false otherwise

## Cycles in Circular Array Problem

public static boolean hasFullLengthCycle(int[] pInputArray){ int currentIndex = 0; int inputLength = pInputArray.length; } for (int numberOfHops = 0; numberOfHops < (inputLength - 1); numberOfHops++){ currentIndex = (currentIndex + pInputArray[currentIndex]) % inputLength; if (currentIndex == 0) return false; } currentIndex = (currentIndex + pInputArray[currentIndex]) % inputLength; return (currentIndex == 0);

## Cycles in Circular Array Problem

What is the running time of your solution? Space complexity? What if we told you that the average input is an array of one million random integers? What unit tests would you write to verify your solution

## Sunday, January 17, 2010

Big-O Notation

Big O notation is a way that programmers use to determine how the running speed of an algorithm is affected as the input size is increased. Big O is used to bound the worst case running time

When asked about the running time of a solution, you are most likely being asked about Big-O time. However, it never hurts to clarify, or say, in the average case X but in the worst case Y.

## Sunday, January 17, 2010

public static class Node{ public Node next; public Integer value; } public static Node firstCommonNode(Node pList1, Node pList2){ int length1 = listLength(pList1); int length2 = listLength(pList2); if (length1 > length2){ for (int a=0; a < (length1-length2); a++){ pList1 = pList1.next; } } else{ for (int a=0; a < (length2-length1); a++){ pList2 = pList2.next; } } while (pList1 != pList2){ pList1 = pList1.next; pList2 = pList2.next; } return pList1; }

## Sunday, January 17, 2010

next value next value next value next value null

next previous value head null next previous value next previous value next previous value next previous value null

## Determining if a linked list has a cycle

head next value next value next value next value

What is the running time of your solution? Space complexity? What if we allowed you to mark nodes?

## Interview Problem Solving Tips

Think out loud Ask for clarications Suggest multiple solutions and/or approaches If you get stuck, dont get frustrated. Instead, ask for a hint

## Pseudocode Writing Tips

Check edge cases Check off by one errors Dont stress the syntax

## General Interview Tips

Familiarize yourself with the companys products Understand the job description Dress appropriately given the companys culture If possible, nd out info about the team or people that youll be interviewing with Be on time