wu :: forums
« wu :: forums - appending a file »

Welcome, Guest. Please Login or Register.
May 18th, 2024, 7:20pm

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: Eigenray, towr, Icarus, ThudnBlunder, Grimbal, SMQ, william wu)
   appending a file
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: appending a file  (Read 584 times)
ic10503
Junior Member
**





   


Gender: male
Posts: 57
appending a file  
« on: Jun 25th, 2008, 3:02pm »
Quote Quote Modify Modify


There are two files A and B both of sizes 5 GB each in a 10 GB harddisk.. You have some amount of ram space. Given functions for opening-closing a file, seeking anywhere in a file and a function truncate() which truncates the last n lines of a file.. We have to develop an algorithm to append file B to file A... please also give the complexity of your solution
IP Logged
TenaliRaman
Uberpuzzler
*****



I am no special. I am only passionately curious.

   


Gender: male
Posts: 1001
Re: appending a file  
« Reply #1 on: Jun 25th, 2008, 3:39pm »
Quote Quote Modify Modify

How are files allocated in the system? Is insertion before the first location allowed?
 
Assuming, we can only write to the end of a file, the process can be divided into,
1. Fill the ram with last 'n' lines of B (however many fits)
2. Truncate those lines from B
3. Write a file C in where the lines are in reverse order
4. Repeat till the entire B is removed and its reverse ordered C is obtained
5. Fill the ram with last 'n' lines of C (however many fits)
6. Truncate those many lines from C
7. Write those lines to A in reverse order
8. Repeat the process till the entire C is removed and you are left with file A which is `cat A B`.
 
If we can insert records before the first line, the reversal can avoided by prepending A to B.
 
Time complexity :
#lines(B) = m
readtime_of_n_lines = t1
writetime_of_n_lines = t2
Then, ~ O((m/n) (t1 + t2) + m)
 
-- AI
IP Logged

Self discovery comes when a man measures himself against an obstacle - Antoine de Saint Exupery
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board