Vision Interview Algorithm Problem--foldersize

Source: Internet
Author: User

Describe

When a file is stored on a disk, it is usually the cluster method. Each cluster has a fixed size, and the amount of space consumed by a file is always an integer multiple of the cluster size. Therefore, if the size of the cluster is 100 bytes, 165 bytes of files will be used to actually use 200 bytes of storage space, resulting in 35 wasted space.

A folder will have multiple file, each of which calculates a wasted space separately, and if a folder does not appear, a wasted space of 0

Defined

Method signature:int[] Calculatewaste (string[] files, int foldercount,int clustersize)

Input parameter Description:

Files is the input folder number and an array of the corresponding file size. For example {"0 55", "0 47"}, denoted as folder 0 below there are two files, the size is 55 and 47, separated by a space.

Foldercount is the number of all the folder. When the Foldercount is 3, there will be 0,1,2 three folder number, Clustersize is the fixed block size.

Return value: The amount of wasted space each folder corresponds to

Test Cases

files={"0 55", "0 47", "1 22", "1 21"}. Foldercount=3,clustersize=50

returns:{48,57,0}

--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------

Amway Two song "On the River Bridge" "Seven month" before solving the problem

Today before the interview bag night led to their two eyes dim, see the problem puzzling to half dead (I send four I will never bag night!) )。 Let 20min write out, and later found that it is not difficult. Of course, the code is wrong, but also to weigh

Core: Converting a string to a number

int* Calculatewaste (string* files,intFoldercount,intclustersize) {    int* waste=New int[Foldercount]; intfilenum=sizeof(Files)/sizeof(files[0]); intFolderid=0, filesize=0; stringtemp; if(files==null| | foldercount==0|| clustersize==0) {cout<<"parameter Error"<<Endl; returnwaste; }     for(intI=0; i<filenum;i++) {Temp=Files[i]; intj=0;  while(temp[j]!=' '){//Calculate FolderIDfolderid=folderid*Ten+temp[j]-'0'; J++; } J++;//Jumping Spaces         while(j<temp.length ()) {FileSize=filesize*Ten+temp[j]-'0'; J++; } Waste[folderid]+ = (filesize/clustersize+1) *clustersize-filesize; FolderID=0; FileSize=0;//Scan the next file before emptying the previous data    }    returnWaste;}

Code as above. For the interviewer to see clearly, I also copied a new copy, however ... Do not say.

About this question and the classmate discussed a bit:

1. To release the memory, in fact, it is not necessary. Because the memory returned must be useful, you need to use it later, so you do not have to free up memory. When the program is finished, the system will do the release for you.

The memory of the dynamic application must remember the release is used for what situation? In a class, if you define a class that has dynamically requested memory in your class, you must release memory in your destructor. (Do not know if I am wrong in this view?) )

2. Also on string [] files, I use sizeof (files)/sizeof (Files[0]) This method to find the array length is based on each element is equal length, then if the string array is not long how do I get the length? The "length parameter" should be given at this time.

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- --------

There were some questions I didn't really think about during the interview, so the answer was not good:

1. Why did you test seu?

2. Why not continue to do QA (QA is actually difficult, also requires very high technology, tdd--testing driven develope)

3. Write your own algorithm code must be fine, it is best to "multiple functions" to complete the function

4.BAT cast, the result how (inner OS: This problem I really do not know how to answer AH!! )

Vision Interview Algorithm Problem--foldersize

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.