Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7062853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:38:38+00:00 2026-05-28T04:38:38+00:00

In an interview this is the question that is asked: You have to write

  • 0

In an interview this is the question that is asked:

You have to write a program that take a directory name (e.g. D:\XYZ)
and a regular expression (e.g. “Olivea“) as an argument. And it will
list down all the files in the given directory with the name matched
with the regular expression.

I have made a simple program from this:

package temp;

import java.io.File;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileSearch1 {

    static ArrayList l = new ArrayList();

    public static void main(String[] args) {

        String folderName = "D:\\"; // the folder path in which you want to
                                    // search
        String strPattern = "Olivea"; // Pattern what you want to search
        final boolean searchinFile = true; // if you want to search in file keep
                                            // it true for folder search keep it
                                            // false
        File f = new File("D:\\");
        Pattern pattern1 = Pattern.compile(strPattern);
        sunny(f, pattern1, searchinFile);

    }

    public static void sunny(File f, Pattern pattern1,
            final boolean searchinFileOnly) {
        File[] f1 = f.listFiles();
        if (f1 == null)
            return;
        int k = f1.length;
        // System.out.println(k);
        int i = 0;
        while (i < k) {
            File f2 = f1[i];
            if (f2.isDirectory() && !searchinFileOnly) {
                Matcher match1 = pattern1.matcher(f2.getName());
                while (match1.find()) {
                    l.add("");
                    System.out.println(f2.getName());
                }
            } else if (f2.isFile() && searchinFileOnly) {
                Matcher match1 = pattern1.matcher(f2.getName());
                while (match1.find()) {
                    l.add("");
                    System.out.println(f2.getName());
                }
            }

            sunny(f2, pattern1, searchinFileOnly);
            i++;
        }
    }
}

After the written test, the interviewer asked me that which design
pattern could you use in this
. Would you please suggest me the various
design patterns that we can implement in this code.?

Please help me how to apply composite and iterator pattern in this? Help will really be appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:38:39+00:00Added an answer on May 28, 2026 at 4:38 am

    Ignoring the fact that you have missed the No.1 Golden Rule of coding, namely that everything should be named correctly, you could use the Iterator and Composite patterns.

    Composite would allow files and directories to be treated similarly.

    Iterator is used to traverse a container and access the container’s elements. The iterator pattern decouples algorithms from containers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yesterday in my interview I was asked this question. (At that time I was
This question was asked at interview. Say I have a contract. [ServiceContract] public interface
This question was asked in my interview. random(0,1) is a function that generates integers
I was asked this question in a .net/C# interview: If we have two threads
I was asked this question in an interview. If there is a pointer that
I was asked this question in a job interview recently. I answered that I
I was asked this question in some interview. I was required to write code
This was a question that was asked to my friend in a Google interview
This question was asked in interview and deals with recursion/backtracking. Suppose we have two
I have a question. I was once asked this in the interview -- Suppose

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.