Amazon Interview Question for Principal Software Engineers


Country: United States
Interview Type: Written Test




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

open FILE,'<',"cppfile.cpp" or die $!;
@lines=<FILE>;
close FILE;
open FILE,'>',"cppfile.cpp" or die $!;
my $commentblock=0;

foreach(@lines)
{

  $line=$_;
  if($line =~ s/^\/\/.*$// )
  {
        next;
  }

  elsif ($line =~ /^\/\*.*/)
  {
        $commentblock=1;
        next;
  }
  elsif( $line =~ /.*\*\/$/ && $commentblock)
  {                            
        $commentblock=0;       
        next;
  }
  elsif($commentblock)
  {
        next;
  }

  print FILE $line;
}

- Amulya June 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks for the solution....!!!!!

Can you please tell me if i need to put comment instead of remove then how can i do that?

- ayush kabra July 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#!/usr/bin/perl -w

use strict;

my $lines;
open(IN, "c++code.txt") or die("$!\n");
while(defined($lines = <IN>))
{
	$lines =~ s/\/\///g;
	$lines =~ s/\/\*//g;
	$lines =~ s/\*\///g;
	print $lines;
}
close IN;

- lochan.brijesh July 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you can also try this:

# To remove comment from c++ file
my $read = "c++.txt";
open(READ,"<$read");
while(<READ>) {
$_=~ s/\/\*(.*)\*\///g or s/\/\/(.*)\/\///g;
print $_;
}
close READ;

- anushenoy May 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

What you have do is to find // or /**/ not enclose in any quote, use regex for searching the pattern, your issue will be resolved.

- Sarfaraj June 07, 2014 | 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