Google Interview Question for Senior Software Development Engineers


Team: Performance Optimization Team
Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
1
of 1 vote

Here is something in Java.

package com.zeroturnaround.callspy;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 */
public class CareerCup {

    static class IThread implements Runnable{

        static double timing(String url) {
            try {
                URL myUrl = new URL(url);
                long l = System.currentTimeMillis();
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(myUrl.openStream()));
                while ((in.readLine()) != null);
                in.close();
                double spentMS = System.currentTimeMillis()  - l;
                return spentMS/1000;
            }catch (Throwable t){
                return Double.MAX_VALUE ;
            }
        }

        static List<IThread> makePool(int times, String url){
            List<IThread> pool = new ArrayList<>(times);
            for ( int i = 0 ; i < times; i++ ){
                IThread it = new IThread(url);
                Thread t = new Thread( it);
                pool.add(it);
                t.start();
            }
            return pool;
        }

        double timing;

        final String url;

        Thread myThread;

        IThread(String url){
            this.url = url;
            timing = Double.MAX_VALUE ;

        }

        @Override
        public void run() {
            this.myThread = Thread.currentThread();
            timing = timing(url);
        }
    }

    public static void analyze(int times, String url){
        List<IThread> pool = IThread.makePool(times,url);
        // wait for all to complete execution :: poll()
        while ( true ){
            boolean oneAlive = false ;
            for ( IThread t : pool ){
                oneAlive |= t.myThread.isAlive();
            }
            if ( !oneAlive ){ break; }
        }
        List<Double> data = new ArrayList<>();
        for ( IThread t : pool ){
            data.add(t.timing);
        }
        // now we have the data, do mean, sd, what not...
        Collections.sort(data);
        int _90 = (int)Math.floor(times * 0.9);
        System.out.printf("90p is : %f", data.get(_90 ) );
    }

    public static void main(String[] args){
        analyze(20, "give your url here" );
    }
}

Please get over Java. It shows you how lame as a language it really is.

- NoOne June 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Based on @ChrisK s comment, I have heavily commented the code. @ChrisK, start using ZoomBA. :-)

/* Make times calls to print stats on a static url 
Making it heavily documented so that ChrisK
can read ZoomBA. It is trivial, once you master 
what -> , :: , $ are.
-> is "as" that is a mapper function 
:: is "where" that is a predicate, a condition 
$ is the iteration variable, holding 
$.o -> object of iteration , the item 
$.i -> the index of the current iteration 
$.c -> the context, the iterable on which iteration is happening 
$.p -> partial, the result of the iteration, as of now 
*/
def analyze ( url, times=10) {
  percentiles = [ 0.99, 0.95, 0.9 , 0.5, 0.1 ] 
  // an inner function 
  def timing( url ){
    // gets the pair, timing in sec, output of the call 
    // using clock statetement 
    // which has the read function to readf from the url 
    #(t,o) = #clock { read ( url ) }
    t // return value are implicit like scala, no point saying return 
    // side effect, there is really no truly void function in ZoomBA 
  }
  def parallelize( url , times ){
    // create a list of *times* threads 
    // each thread has a body of function call timing()
    // zoomba threads have a field :value, which stores the thread functions return value 
    // this is massive improvement from Java, see similar code and the pain below 
    threads = list([0:times] ) -> { thread() -> { timing( url ) } }
    // polls num tries, poll-interval, until condition is true
    // returns true if condition was true, else returns false  
    // :: (such that) is a short form of where clase in zoomba
    // the code waits till no alive thread 
    // shorthand of java function isXxx() -> xxx in  zoomba
    // making it way more succint 
    // could easily be done using a .join() but, why care?
    poll(300,50) :: { !exists( threads ) :: { $.o.alive } }
    // extracting the return value of the threads into another list 
    // -> is the 'as' symbol 
    // it reads create a list from ( threads ) as ( mapping ) thread.value as the item 
    // $ is the iteration construct, more like *this* for a loop.
    // $.o stores the iteration object for the loop, this this case, a thread object 
    list( threads ) -> { $.o.value }
  }
  def stats( data ){
    // sum 
    mean = sum(data) / size(data)
    // sum over item - mean whole squared, right?
    variance = sum( data ) -> { ($.o - mean) ** 2 }
    sd = variance ** 0.5 
    printf( 'mean: %s\n', mean )
    printf( 'sd: %s\n', sd )
    // now percentile calculations
    // sorta --> sort ascending the data values 
    sorta(data) 
    // another iteration - for() would be same 
    fold ( percentiles ) ->{
      printf( '%.2fp: %s\n', $.o , data[floor( size(data) *  $.o )] )
    }
  }
  println('url -> ' + url )
  println('num of times -> ' + times )
  println('All in secconds.')  
  data = parallelize( url, times )
  stats(data)
}

- NoOne June 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Something in Java, would have helped understand better

- xyz June 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I agree, though I would implement this using `ThreadPoolExecutor` as they mentioned "Imagine your are coding for production ready code"

- Anonymous June 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I agree, though I would implement this using `ThreadPoolExecutor` as they mentioned "Imagine your are coding for production ready code"

- Anonymous June 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@NoOne: I have trouble reading Zoomba :-) but agree with your comment about Java. Python is somewhat of a compromise, I feel...

- Chris June 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What would be limitations and edge cases to this question ?

- Anonymous June 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What would be edge cases and limitations to this question ?

- Anonymous June 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There are certain edge cases to this code. Why to check for the entire response. Why not just check the response code of '200'. This might save some time. As this is "Performance Team", I bet the requirements would be high enough to provide production quality code

- Mark June 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There are certain edge cases to this code. Why to check for the entire response. Why not just check the response code of '200'. This might save some time. As this is "Performance Team", I bet the requirements would be high enough to provide production quality code

- Mark June 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There are certain edge cases to this code. Why to check for the entire response. Why not just check the response code of '200'. This might save some time. As this is "Performance Team", I bet the requirements would be high enough to provide production quality code. Though the question is simple I agree, but the answer would require handling edge cases, such as checking if the remote is reachable, checking the response code for 200 and others

- npkatre104 June 14, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More