Directi Interview Question for SDE1s


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
1
of 1 vote

For a character X in the string

1) let p(X) be its predecessor, if X is not the first, or the last character otherwise;

2) let s(X) be its successor, if X is not the last, or the first character otherwise.

For each character X (from first to last), if X = '?' then replace it with the first of 'a','b' or 'c' that is distinct from p(X) and s(X). At each iteration check also whether X is distinct from p(X) and s(X) and, if not, output "Not Possible" and stop.

#include <iostream>
#include <string>

void next_string(std::string& str) {
  
  for (unsigned i = 0; i < str.size(); ++i) {
      
    char const p = (i == 0 ? str.back() : str[i - 1]);
    char&      x = str[i];
    char const s = (i == str.size() - 1 ? str.front() : str[i + 1]);
    
    if (x == '?') {
      
      if (p != 'a' && s != 'a')
        x = 'a';
      
      else if (p != 'b' && s != 'b')
        x = 'b';
      
      else
         x = 'c';
    }
    
    else if (x == p || x == s) {
      std::cout << "Not Possible\n";
      return;
    }
  }
  
  std:: cout << str << '\n';
}

- cassio.neri August 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For every ? you have three choices (a,b or c), but a maximum of two constraints(the elements on left and right), so at every step you always have a choice.
Just go in order a,b and c and replace ? with whatever fits the constraints at the given location first.

- Doesitmatter August 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

s=list(raw_input())
f=False

for i in range(0,len(s)):
	if s[i]=='?':
		p=s[i-1]
		q=s[i+1]
		f=True
		if p!='a' and q!='a':
			s[i]='a';
		elif p!='b' and q!='b':
			s[i]='b';
		else:
			s[i]='c';
if f:
	print ''.join(s);
else:
	print "Not Possible"

- harshit.knit October 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is same as the prev one

- chandan September 01, 2015 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More