Scripting Interview Question | Physical Design | VLSI

Scripting Interview Question

Role: Physical Design

Company : Product Based

Experience: 5+ years

Write a program or suggest an algorithm that compares two files containing lines of code. The program should perform the following tasks:

  1. Consider all possible solutions and identify the most efficient one.
  2. Identify and report matching patterns between the two files.
  3. Identify and report non-matching patterns between the two files.
  4. Propose a solution for scenarios where the file sizes are small (hundreds of lines of code).
  5. Propose a solution for scenarios where the file sizes are large (thousands of lines of code).
  6. Discuss whether the same solution works for both small and large file sizes.

Instructions for Submission:

  • Please post your answers in the comment section below.
  • For detailed explanations and solutions, check the answers later on compile-vlsi.blogspot.com

Answer: (Updated on 01 Aug 2024)

Approach 1: Brute Force

Description:

  • Use two nested loops to compare each line of the first file with each line of the second file.
  • Time Complexity: O(N^2), where N is the number of lines in each file.

Drawback:

  • Not efficient for large files due to quadratic time complexity.

Approach 2: Using grep

Description:

  • Use Unix grep commands to find matching and non-matching lines.
  • This approach is efficient for small to moderately sized files and leverages optimized search algorithms in grep.

Commands:

  • To find matching lines: grep -xf file1 file2
  • To find non-matching lines: grep -xvf file1 file2

Approach 3: Hash-Based Comparison

Description:

  • Use a hash table to store lines from one file.
  • Compare lines from the second file against this hash table.
  • Time Complexity: O(N), where N is the number of lines in each file.

Advantages:

  • More efficient than brute force.
  • Suitable for both small and large files.

#!/usr/bin/perl
use strict;
use warnings;

# Function to read file into a hash
sub read_file_into_hash {
    my ($filename) = @_;
    my %hash;
    open my $fh, '<', $filename or die "Could not open '$filename' $!\n";
    while (my $line = <$fh>) {
        chomp $line;
        $hash{$line} = 1;
    }
    close $fh;
    return %hash;
}

# Read files into hashes
my %file1_lines = read_file_into_hash('file1.txt');
my %file2_lines = read_file_into_hash('file2.txt');

# Identify and report matching and non-matching lines
open my $matching_fh, '>', 'matching.txt' or die "Could not open 'matching.txt' $!\n";
open my $non_matching_fh, '>', 'non_matching.txt' or die "Could not open 'non_matching.txt' $!\n";

foreach my $line (keys %file1_lines) {
    if (exists $file2_lines{$line}) {
        print $matching_fh "$line\n";
    } else {
        print $non_matching_fh "$line\n";
    }
}

foreach my $line (keys %file2_lines) {
    if (!exists $file1_lines{$line}) {
        print $non_matching_fh "$line\n";
    }
}

close $matching_fh;
close $non_matching_fh;


Happy Learning !

#scripting #coding #pd #physicaldesign #pv #semiconductor #vlsi #india #freshers #interview #timing #files #compare #dsa #python #perl #tcl

Comments

Must Read

Understanding of Placement (Physical Design) - Part 1

Interview Question - Physical Design (PD) | Clock Skew

Dealing with Congestion in a Practical Way (Physical Design) :

LVS Issue | Physical verification | VLSI

Port Punching | Physical Design | VLSI

Low Power Design | Physical Design | Part 1

PPA Optimization in Synthesis & Physical Design | Area | VLSI Design

Placement - Physical Design - Part 2 - General Setup

Longer Routing Length | Tcl Scripting | Routing | Physical Design | VLSI