Google Interview Question for Developer Program Engineers


Country: United States




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

What if there are multiple words- how will writing a last word help

- Anonymous September 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

my $arr="this is for testing";
my @arr1=split(" ",$arr);
print $arr1[$#arr1]; 
or
print $arr1[-1];

- varalakshmi November 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is my code. I have a feeling I'm missing something here, coz this is pure programming logic. Nothing specific to Perl.

use strict;

my $LIMIT = 9;

my $file_name = "abc.txt";

open(FILE,"<$file_name");

my @lines = <FILE>;
my @new_lines = ();
my $new_ind = 0;

foreach my $line (@lines) {
	
	if( length($line) > $LIMIT ) {
		
		# Split it
		my @words = split(" ",$line);
		my $frame = "";
		my $count = 0;
		
		foreach my $word ( @words ) {
			
			$count += (length($word));
			
			if( $count > $LIMIT ) {
				
				$new_lines[$new_ind] = $frame . "/\n";
				$new_ind++;
				$frame = "";
				$count= (length($word) + 1);
			} 
			$frame .= ($word . " ");
			
		} # end foreach my $word
		$new_lines[$new_ind] = $frame . "\n";
		
	} else {
		# Add it to new array
		$new_lines[$new_ind] = $line;
	}
	$new_ind++;
	
} # end foreach $lines

# Write new lines into the old file
close( FILE);
open(FILE,">xyz.txt");
print FILE @new_lines;
close(FILE);
print "\nDone";

- D September 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#!/usr/bin/env python

import sys

# Input : A Perl program file
# We need to modify the file to have a max of 80 characters per line and create
# a new perl file.  Problem is we need to use "/" wherever we split the line
# and also, the split MUST happen at a place with white space.

def split_line(line, MAX_LIMIT=5):
    """
    split a line, splitter is "/"

    >>> list(split_line("ABCD EF", 5))
    ['ABCD/', 'EF']

    # this one can't be split!
    >>> list(split_line("ABCDEF", 5))
    ['ABCDEF']

    >>> list(split_line("123456789 xyz", 10))
    ['123456789/', 'xyz']
    """

    while len(line) > MAX_LIMIT:

        # split line into words
        words = line.split(" ")
        count = 0
        index = 0

        # find the maximum "index" we can afford
        for i in range(0, len(words)):
            count = count + len(words[i]) + 1

            if count >= MAX_LIMIT:
                break

            index = index + 1

        # combines words upto "index" back
        newline = " ".join(words[:index + 1])

        # find the leftover line
        left = words[index + 1:]
        line = " ".join(left)

        # is this the last chunk for this line?
        if left:
            yield newline + '/'
        else:
            yield newline

    if line:  # stop returning empty string
        yield line


if __name__ == "__main__":

    for line in sys.stdin:
        line = line.rstrip()

        for l in split_line(line):
            print l

- anon October 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use strict;
use warnings;

my $limit = 80;
my $NF;
open($NF, ">limit_op.txt");
while (<>) {
	my $line = $_;	
	do {		    
	    if ($line =~ /^(.{0,$limit})(?:(\s+)(.*)$|$)/) {		
		print $NF $1;
		$line = $3;
		print $NF "/\n$2" if(length($line) > 0);		
	    }	 	
	} while (length($line) > 0);
	print $NF "\n";
}
close($NF);

- Sathish J October 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you explain the code please? :) What does the "?:" in regex do?

- D October 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

github.com/vikhyath/python-runs/tree/master/code-formatter

- vikre October 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As simple python logic is as follows :

>>> line = "ABCDEF XYZ";
>>> LIMIT = 5
>>> newLines = ""
>>> while(len(line) > LIMIT) :
...     index = line.rfind(' ', 0, LIMIT)
...     if index == -1:
...         index = line.find(' ', LIMIT)
...     if index == -1:
...         newLines = newLines + line
...         line = ''
...     else :
...         newLines = newLines + line[0:index] + '/\n'
...         line = line[index:]
...
>>> newLines = newLines + line
>>> print newLines
ABCDEF/
 XYZ

in case you want to remove the whitespace while splitting you need to change the to the below line in the code :
line = line[index:] becomes line = line[index+1:]

- Rage October 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A solution in Python

f = open('file.pl', 'r')
f2 = open('file2.pl', 'w')
lines = f.readlines();

for line in lines:
    if len(line) > 80:
        words = line.split()
        line_len = 0
        for word in words:
            line_len += len(word)+1 # Plus 1 because of the whitespace
            if line_len > 80:
                f2.write('/\n')
                line_len = 0
            f2.write(word + ' ')
    else:
        f2.write(line.strip()) # Strip to remove the \n
        
    f2.write('\n') # New line for all the lines. If a line is more than 80 chars, when we split it the remaining characters will be in their own line

- Aaron October 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is the simple method I could find.

sub split($)
{
$line=shift;
@char_set=split('',$line);
$BUFFER='';

foreach $c (@char_set)
{
	if(($c==' ') && (++length > $MAX_LENGHT - 1))
	{
		print OF $BUFFER."\\"."\n";
	}
	else
	{
		$BUFFER=$BUFFER.$c;
		$length++;
	}
}
}

- Aditya Kulkarni December 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#!/usr/bin/perl -w
open(SRCFILE,'src.txt');
open(DESTFILE,'>dest.txt');

#a sub to do the formating if number of characters is greater than xx
sub formatLine{
@words = split(' ',$_);
foreach $word(@words){
$len += length($word)+1;
if($len > 80){
print DESTFILE "\n";
$len = 0;
}
print DESTFILE $word," ";
}
print DESTFILE "\n";
}

while(<SRCFILE>){
chomp;
if(length($_) < 80){
print DESTFILE $_."\n";
}
else{
formatLine($_);
}
}
close(SRCFILE);
close(DESTFILE);

- Hagos February 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A perfectly working one...modified my previous post

#!/usr/bin/perl -w
open(SRCFILE,'src.txt');
open(DESTFILE,'>dest.txt');

#a sub to do the formating if number of characters is greater than xx
sub formatLine{
        $len = 0;
        @words = split(' ',$_);
        foreach $word(@words){
                $len += length($word)+1;
                if($len > 80){
                        print DESTFILE "\n";
                        $len = length($word)+1;
                        }
                print DESTFILE $word," ";
        }
        print DESTFILE "\n";
}

while(<SRCFILE>){
        chomp;
        if(length($_) < 80){
                print DESTFILE $_."\n";
        }
        else{
                formatLine($_);
        }
   }
close(SRCFILE);
close(DESTFILE);

- Hagos February 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#!/usr/bin/perl
use warnings;

my $MAXLINELENGTH = 50;

open FD, 'input.txt';
while (<FD>) {
  chomp(my @line = split '', $_);
  my $lineLenght = @line;
  #print "$lineLenght\n";
  if ($lineLenght > $MAXLINELENGTH ) {
    my $numlines = $lineLenght / $MAXLINELENGTH;
    #print "<$numlines><$lineLenght><$MAXLINELENGTH>\n";
    my $line = 1;
    for (my $count = 0; $count <= $numlines; $count++) {
      my $startPos = $MAXLINELENGTH * $count;
      my   $endPos = $MAXLINELENGTH * $line - 1; 
      my $printline = join('', @line[$startPos..$endPos]);
      my $delimiter = $line <= $numlines ? '\\' : "";
      print "$printline $delimiter\n";
      $line++;
    }
  }
}

- karthik June 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The Simplest Possible Correct Perl Solution for the given problem.

#!/usr/bin/perl -w

open(SRCFILE,'/home/oracle/script/data_file/pp1_input.txt');

while(<SRCFILE>) {
chomp($_);
$line.=$_;
}

$ll = length($line);
print "$ll\n";


$i=0;
while($i < $ll) {
$NL = substr $line, $i, 80;
$NL.=" /";
push @NL, $NL;
$i = $i + 80;
}
foreach (@NL) {
print "$_\n";
}

- @thePuneet_IITD November 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My solution in perl using recursion

use warnings;
use strict;

open(FH, "infile.txt");
open(FH2, " > outfile.txt");
my @array=<FH>;
foreach (@array) {

        process($_);
}

sub process {
my $line=shift;
my @tmp;
my $i=0;
$line=~s/([\d\w\W])/$tmp[$i]=$1;$i++;$1;/ge;##Get every word number symbol and space
        if(scalar(@tmp)>80) {
                my $count=80;
                while($tmp[$count] ne " ") {
                        $count--;
                }
                if($count==80) {
                $count=$count-2; ##Take care of "/" and white space
                }
                for(my $j=0;$j<$count;$j++) {
                        print FH2 $tmp[$j];
                }
                print FH2 "\/";
                print FH2 "\n";
                my $rem="";
                for (my $j=$count+1;$j<scalar(@tmp);$j++) {
                        $rem.=$tmp[$j];
                }
                $i=0;
                @tmp="";
                process($rem);
        }
        else {
                print FH2 @tmp;
        }

}

- Sughosh Divanji July 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you can also use range operators to solve this problem

my $read = "a.txt";
my $write = "a_modified.txt";
open(READ,"<$read");
open(WRITE,">$write");
while(<READ>) {
 chomp $_;
 my @arr = split(//,$_);
 if (scalar(@arr)>= 80) {
  print WRITE @arr[0 ..79],"\\"."\n";
  print WRITE @arr[80 .. $#arr],"\n";
 } else {
  print WRITE "$_\n";
 }
 
}
close READ;
close WRITE;

- Anuradha Shenoy April 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you can also use range operators to solve this problem

my $read = "a.txt";
my $write = "a_modified.txt";
open(READ,"<$read");
open(WRITE,">$write");
while(<READ>) {
 chomp $_;
 my @arr = split(//,$_);
 if (scalar(@arr)>= 80) {
  print WRITE @arr[0 ..79],"\\"."\n";
  print WRITE @arr[80 .. $#arr],"\n";
 } else {
  print WRITE "$_\n";
 }
 
}
close READ;
close WRITE;

- anushenoy April 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Code in Python:

def main():
    temp = ''
    count = 0
    with open ('story.txt','r') as f:
        for line in f.readlines():
            for char in line:
                if char != ' ':
                 temp = temp + char
                 count += 1
                else:
                    count += 1
                    if count < 80 :
                        with open('new_story.txt','a') as fh:
                            fh.write(temp + ' ')
                        temp = ''
                    else:
                        with open('new_story.txt','a') as fh:
                            fh.write('/' + '\n' + temp + ' ')
                        count = len(temp) + 1
                        temp = ''
                        
   

if __name__ == "__main__":
    main()

- PS September 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Algorithm can be following:
1. Read a file line by line & repeat step 2 - 7
2. Get the length of the line
3. If it is greater than 80 then split the line by white space & create array of words
4. Get the last element of the array
5. Write the array from 0 to (length - 1) including white space after every word
6. Write the "/" at the end
7. Write the last word of the array in the new line

- Anonymous September 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you please provide solution with some code?

- Anonymous October 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if you the last 2 (or 3, 4 ...) words in array needed to be in the second line?

- Waqar November 06, 2013 | Flag


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