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 6089341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:05:19+00:00 2026-05-23T12:05:19+00:00

I was asked a question in an interview. The question was that the input

  • 0

I was asked a question in an interview. The question was that the input string (which might come from email or large text file) needs to be filtered with list of bad words and replaced with something else.

for example, if the input string contains bad words which exist in a list of bad words, the bad word needs to be replaced with something else, something like empty string or wild card character.

The solution that I came up with was to put all the bad words in a hashmap and put input string in a StringBuffer and retrieve word by word delimited by space and check if word exists in hashmap, if it exists, replace the word with empty string. But the interviewer said that manipulating StringBuffer might be expensive, because stringBuffer maintains an array of characters. Replacing means it needs to copy to a new array.

Does anybody have a better algorithm instead of this solution?

Thanks.

  • 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-23T12:05:20+00:00Added an answer on May 23, 2026 at 12:05 pm

    you can first parse the String into String[] (using split()) and, iterate over the words and check if a word is needed to be replaced by looking for it in your HashMap<String,String> of blacklisted words. (if it is, you just replace the reference with the ‘replace to’ [the value of the String in the map], and do not create a new string). then, using StringBuilder, rebuild your new string.

    should look something like that:

    public static String replaceString(Map<String,String> map,String input) { 
        String[] arr = input.split("\\s");
        for (int i = 0;i<arr.length;i++) { 
            String val = map.get(arr[i]);
            if (val != null) arr[i] = val;
        }
        StringBuilder sb = new StringBuilder();
        for (String s : arr) {
            if (s == null || s.length() == 0 ) continue;
            sb.append(s).append(' ');
        }
        return sb.toString().trim();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In an interview I was asked a question that I'd never thought about, which
I came across a interview question that asked to remove the repeated char from
Yesterday in my interview I was asked this question. (At that time I was
This was the question asked in interview. Can we call one constructor from another
This question was asked in my interview. random(0,1) is a function that generates integers
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 had an interview question that asked me for my 'feedback' on a piece
i was asked this question in my interview that how to make a class
Got asked this interesting interview question today. Explain in detail the process by which

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.