Extract Characters at Prime Indices

Given a string 'STR' of length 'N', return a new string containing all characters that are located at prime indices in the original string. Ensure that the order of characters in the new string is the same as their order in the original string.

Example:

Input:
STR = "abcdefg"
Output:
"bdf"
Explanation:

Characters at prime indices 2, 3, and 5 are 'b', 'd', and 'f', respectively.

Constraints:

  • The first character of the string is at index 0 (0-based indexing).
  • Prime indices are those indices that are prime numbers. For example, if a character is at index 5, it is at a prime index because 5 is a prime number.
  • The string contains characters from 'a'-'z', 'A'-'Z', and '0'-'9'.
AnswerBot
4mo

The task is to extract characters at prime indices from a given string and return them in the same order.

  • Iterate through the string and check if the index is prime.

  • Use a helper function to determine i...read more

Help your peers!
Select
Add answer anonymously...
Accenture Data Engineering Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits