• cole (unregistered) in reply to Earl Purple

    What, like this?

    #include <iostream>
    using namespace std;
    void main(){
    cout << "      1      2    Fiz      4    Buz\n";
    cout << "    Fiz      7      8    Fiz    Buz\n";
    cout << "     11    Fiz     13     14 FizBuz\n";
    cout << "     16     17    Fiz     19    Buz\n";
    cout << "    Fiz     22     23    Fiz    Buz\n";
    cout << "     26    Fiz     28     29 FizBuz\n";
    cout << "     31     32    Fiz     34    Buz\n";
    cout << "    Fiz     37     38    Fiz    Buz\n";
    cout << "     41    Fiz     43     44 FizBuz\n";
    cout << "     46     47    Fiz     49    Buz\n";
    cout << "    Fiz     52     53    Fiz    Buz\n";
    cout << "     56    Fiz     58     59 FizBuz\n";
    cout << "     61     62    Fiz     64    Buz\n";
    cout << "    Fiz     67     68    Fiz    Buz\n";
    cout << "     71    Fiz     73     74 FizBuz\n";
    cout << "     76     77    Fiz     79    Buz\n";
    cout << "    Fiz     82     83    Fiz    Buz\n";
    cout << "     86    Fiz     88     89 FizBuz\n";
    cout << "     91     92    Fiz     94    Buz\n";
    cout << "    Fiz     97     98    Fiz    Buz\n";
    }
  • Khanmots (unregistered) in reply to cole

    As long as we're being silly...

    #include<stdio.h>
    int main(void)
    {
      for(int i = 5; i <= 100; i+=9)
      {
        printf("%d\n%d\n%buz\n%d\n%s\n", --i, --i, i--, i--%3?"fiz":"fizbuz");
      }
      return 0;
    }
  • bigfoot (unregistered) in reply to Mister Bee
    <snip previous stuff/>

    I wouldn't hire you either, for deliberate and malicious over-engineering and willful under-use of ternary operators:

    ..snip..
                foreach (Match m in new Regex(subtext.Text, RegexOptions.IgnoreCase).Matches(text.Text))
                    resultText.Append((resultText.Length > 0 ? "," : "") + ((m.Index + 1).ToString()));
    ..snip..
    
    [/quote]

    It wasn't deliberate, it was entitely accidental....so can I have the job????

  • NoWayDude (unregistered)

    Since the fizbuzzing it getting stranger, here it is in Sieve of Eratosthenes style:

    #!/usr/bin/env python
    n = 100
    f = [False]*n
    
    for i in range(0,n,3):
        f[i] = 'Fiz'
    
    for i in range(0,n,5):
        if f[i]:
            f[i] = 'FizBuz'
        else:
            f[i] = 'Buz'
    
    for i in range(n):
        if f[i]:
            print i,f[i]
    

    If you really dig loop unrolling, there's this (although it runs past the 100 mark):

    #!/usr/bin/env python
    for i in range(0,100,15):
        print   i, "FizBuz"
        print i+3, "Fiz"
        print i+5, "Buz"
        print i+6, "Fiz"
        print i+9, "Fiz"
        print i+10, "Buz"
        print i+12, "Fiz"
    
  • RF (unregistered) in reply to Fanguad

    The question isn't really specific enough though to expect such a direct answer. The expectation is that the interviewee will somehow figure that they're two points on the Cartesian plain, and two-dimensional as well. It's OK if you're interviewing people without exposure to upper-level undergraduate university math, but otherwise I'm not sure whether it's a valid way of telling whether someone is 'smart' enough to work for you. Maybe this type of thing is the reason why Microsoft doesn't ask silly questions along with their interviews anymore.

  • Anonymous Scheme weenie, thinking of registering... (unregistered) in reply to Khanmots
    Khanmots:
    As long as we're being silly...
    #include<stdio.h>
    int main(void)
    {
      for(int i = 5; i <= 100; i+=9)
      {
        printf("%d\n%d\n%buz\n%d\n%s\n", --i, --i, i--, i--%3?"fiz":"fizbuz");
      }
      return 0;
    }

    Impressive parameter stack abuse, but "buz" is being printed out every fifth number starting at 3 (3, 8, 13, 18, etc.) instead of every third number. I'd like to see what this looks like if you can further twist it to get correct output.

  • Khanmots (unregistered) in reply to Anonymous Scheme weenie, thinking of registering...
    Anonymous Scheme weenie:
    Khanmots:
    As long as we're being silly...
    #include<stdio.h>
    int main(void)
    {
      for(int i = 5; i <= 100; i+=9)
      {
        printf("%d\n%d\n%buz\n%d\n%s\n", --i, --i, i--, i--%3?"fiz":"fizbuz");
      }
      return 0;
    }

    Impressive parameter stack abuse, but "buz" is being printed out every fifth number starting at 3 (3, 8, 13, 18, etc.) instead of every third number. I'd like to see what this looks like if you can further twist it to get correct output.

    Argh... you are indeed right. Can't believe I made that mistake :(

  • cole (unregistered) in reply to cole
    cole:
    akatherder:
    Most interviewers will look at the FizBuz question with 4 possible outcomes:
    1. You have no idea what you're doing and can't come up with a decent solution.
    2. You are a novice and have three different cases. One for Fiz, one for Buz, and one for FizBuz.
    3. You are competent and only need to use the two cases.
    4. You are eloquent and come up with a solution, they don't quite understand but it seems to work.
    How praytell do you do it in two? You have three test cases. 3, 5, 15 <- counts up to three cases in my book, so how do two tests determine if it's three states? Granted, my original reply also assumed that you don't return an endline after every test, only where you had a result in the first place. but I tried for simplicity rather than breakproof usage.
    I guess the world will never know, huh? I'ld really like to know how to do this in only two cases (which I thought I did)...

    Captcha VALIDUS

  • Anonymous Scheme weenie, thinking of registering... (unregistered)

    Here's a BF implementation, because I'm apparently trying to make myself insane. If it actually worked (I doubt it does and I don't want to try it) it would zero-pad all the numbers, but it's pretty good otherwise.

    ++++++++++++++++++++++++++++++++++++++++++++++++++
    ++++++++++++++++++++++++++++++++++++++++++++++++++
    >+++++
    >++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    >++++++++++++++++++++++++++++++++++++++++++++++++++++
    +++++++++++++++++++++++++++++++++++++++++++++++++++++
    >+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    >+++
    >++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    >++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    >+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    >
    >>>++++++++++
    <<<<<<<<<<
    [ test if counter is at 100 yet
    - take one off the initial counter
    >- take one off the five counter
    [ five counter is nonzero?
    <<<<<<<     jump to constant zero to move on
    ] 
    >[ are we at 'F' or 0 right now?
    >>>>>>>>>>>[-] jump to NZ flag and zero it
    >.>.>.<<<+++++>>>>> print fiz / reset counter to 5 / jump to just before 3
    ]>- take one off the three counter
    [ three counter is nonzero?
    <<      jump to constant zero to move on
    ]
    >[ are we at 'B' or 0 right now?
    >>>>>>[-] jump to NZ flag and zero it
    >.>.>.<<<+++>>>      print buz / reset counter to 3 / jump to just before NZ
    ]
    >> we're at count up
    + inc countup
    < back to NZ
    [  set? print here 
       >    move to countup
    [->>>+>+<<<<]>>>>[-<<<<+>>>>] copy countup to tmp1
    <----------[++++++++++<<-<++++++++++>>>[-]]<<<---------->+> if countup is 10 then increment tens / zero tmp1
    [->>+>+<<<]>>>[-<<<+>>>] copy tens to tmp1
    <----------[++++++++++<-<++++++++++>>[-]]<<---------->+ if tens is 10 then increment hundreds
    ++++++++++++++++++++++++++++++++++++++++++++++++. print hundreds as a digit
    ------------------------------------------------<
    ++++++++++++++++++++++++++++++++++++++++++++++++. print tens as a digit
    ------------------------------------------------<
    ++++++++++++++++++++++++++++++++++++++++++++++++. print countup as a digit
    ------------------------------------------------<
       [-]  reset NZ to zero
    ]       leave branch
    +       set NZ at 1 again
    >>>>>>.  print nl
    <<<<<<<<<<<<<<<<<<<]  end program loop at first array pos
    
  • cole (unregistered) in reply to Anonymous Scheme weenie, thinking of registering...

    Why would you try and code this in BF in the firstplace. You think potential employers have ever even heard of it? (well, the fun places to work they would have, but that's somewhere you'ld want to work, not likely they'll be hiring, is it?)

  • Edward Royce (unregistered)

    Hmmmm.

    It amazes me what kind of weird arguments spring up around here.

  • anonyme christian (unregistered)

    Here's a bf implementation that "should" work:

    ++++++++++[>+>+++++>+++++++>+++++++++++>++++++++++++<<<<<-]>>-.+<.>.<.>>.>----->++.--<<<<.>++.--<.>>----.++++>>---.+++++.--<<<<.>>.>----->++.--<<<.>+++++.-----<.>++++++.------<.>>.>----->++.--<<<<.>>----.++++>>---.+++++.--<<<<.>-..+<.>>.>----->++.--<<<<.>-.++.-<.>-.+++.--<.>>.>----->++.--<<----.++++>>---.+++++.--<<<<.>-.+++++.----<.>-.++++++.-----<.>>.>----->++.--<<<<.>-.++++++++.-------<.>>----.++++>>---.+++++.--<<<<.>>.>----->++.--<<<<.>..<.>.+.-<.>>.>----->++.--<<<<.>>----.++++>>---.+++++.--<<<<.>.++++.----<.>>.>----->++.--<<<<.>.++++++.------<.>.+++++++.-------<.>>.>----->++.--<<----.++++>>---.+++++.--<<<<.>+.--.+<.>+.-.<.>>.>----->++.--<<<<.>+.+.--<.>>----.++++>>---.+++++.--<<<<.>>.>----->++.--<<<<.>+.-+++++.-----<.>+.+++++.------<.>>.>----->++.--<<<<.>>----.++++>>---.+++++.--<<<<.>++.---.+<.>>.>----->++.--<<<<.>++.-.-<.>++..--<.>>.>----->++.--<<----.++++>>---.+++++.--<<<<.>++.++.----<.>++.+++.-----<.>>.>----->++.--<<<<.>++.+++++.-------<.>>----.++++>>---.+++++.--<<<<.>>.>----->++.--<<<<.>+++.---.<.>+++.---+.-<.>>.>----->++.--<<<<.>>----.++++>>---.+++++.--<<<<.>+++.+.----<.>>.>----->++.--<<<<.>+++.+++.------<.>+++.++++.-------<.>>.>----->++.--<<----.++++>>---.+++++.--<<<<.>++++.-----.+<.>++++.----.<.>>.>----->++.--<<<<.>++++.--.--<.>>----.++++>>---.+++++.--<<<<.>>.>----->++.--<<<<.>++++.+.-----<.>++++.++.------<.>>.>----->++.--<<<<.>>----.++++>>---.+++++.--<<<<.>+++++.------.+<.>>.>----->++.--<<<<.>+++++.-----+.-<.>+++++.---.--<.>>.>----->++.--<<----.++++>>---.+++++.--<<<<.>+++++.-.----<.>+++++..-----<.>>.>----->++.--<<<<.>+++++.++.-------<.>>----.++++>>---.+++++.--<<<<.>>.>----->++.--<<<<.>++++++.------.<.>++++++.-----.-<.>>.>----->++.--<<<<.>>----.++++>>---.+++++.--<<<<.>++++++.+.-------<.>>.>----->++.--<<<<.>++++++..------<.>++++++.+.-------<.>>.>----->++.--<<----.++++>>---.+++++.--<<<<.>+++++++.--------.+<.>+++++++.-------.<.>>.>----->++.--<<<<.>+++++++.-----.--<.>>----.++++>>---.+++++.--<<<<.>>.>----->++.--<<<<.>+++++++.--.-----<.>+++++++.-.------<.>>.>----->++.--<<<<.>>----.++++>>---.+++++.--<<<<.

  • (cs) in reply to Earl Purple
    Earl Purple:
    How about:
    switch( i % 15 )
    {
      case 0:
        std::cout << "buzfiz\n";
        break;
    

    case 3: case 6: case 9: case 12: std::cout << "buz\n"; break;

    case 5: case 10: std::cout << "fiz\n"; break;

    default: std::cout << i << '\n'; }

    And of course run that in a loop.

    You could also create a table with cached values. The real issue is what it would be used for in real life.

    You, sir, are hired. Welcome aboard. Would you like 2 30-inch monitors, or 4 24-inch ones in your new office?

  • Anonymous Scheme weenie, thinking of registering... (unregistered) in reply to cole
    cole:
    Why would you try and code this in BF in the firstplace.

    Because the guy in front of me did it in Befunge, and my Scheme loop and clever use of sprintf to do a real two-case version had slightly less geek cred than that.

    In short, I did it because I can.

  • (cs) in reply to Anonymous Scheme weenie, thinking of registering...
    Anonymous Scheme weenie:
    cole:
    Why would you try and code this in BF in the firstplace.

    Because the guy in front of me did it in Befunge, and my Scheme loop and clever use of sprintf to do a real two-case version had slightly less geek cred than that.

    In short, I did it because I can.

    BF was my initial idea as well, but I decided on befunge in the end.

    Who is going to do a malbolge version? ;) ;)

    And as above: "Because I can" ;) <3 esoteric languages

  • (cs) in reply to cole
    cole:
    akatherder:
    Most interviewers will look at the FizBuz question with 4 possible outcomes: ...
    How praytell do you do it in two? You have three test cases. 3, 5, 15 <- counts up to three cases in my book, so how do two tests determine if it's three states?
    Technically, I make it four cases, but I'll give you that one of them is the default (i.e. none of the above). I can do it with 2 tests for the actual cases and one for the default, but I don't know if that counts as competence or not... ;^) For what it's worth:
    for (i=1; i<101; i++) {
        strout = "";
        if (i%3 == 0) strout += "Fizz";
        if (i%5 == 0) strout += "Buzz";
        if (strout.length == 0) strout += i;
        document.write(i + "
    "); }

    Addendum (2008-05-07 05:55):

    cole:
    akatherder:
    Most interviewers will look at the FizBuz question with 4 possible outcomes: ...
    How praytell do you do it in two? You have three test cases. 3, 5, 15 <- counts up to three cases in my book, so how do two tests determine if it's three states?
    Technically, I make it four cases, but I'll give you that one of them is the default (i.e. none of the above). I can do it with 2 tests for the actual cases and one for the default, but I don't know if that counts as competence or not... ;^) For what it's worth:
    for (i=1; i<101; i++) {
        strout = "";
        if (i%3 == 0) strout += "Fizz";
        if (i%5 == 0) strout += "Buzz";
        if (strout.length == 0) strout += i;
        document.write(i + "
    "); }
    --- Addendum --- As an aside, I had an interview for a web developer on Friday, and at one point the tech interview started talking about web applications being stateless, and asked me how I would manage that state. I made a few nervous points about session variables / objects and data validation on the server side; he obviously wasn't getting what he wanted and prompted me about how to implement some state - so I suggested he was talking about AJAX and other asynchronous technologies. He stillw asn't getting the reply he wanted, so he started talking at a more and more basic level about how you transfer the information, and I suddenly realised that he was asking if I knew how to use GET and POST to send information requests to the server. I couldn't decide what the biggest WTF was - him talking about that in terms of managing state, or him actually asking a question to which the answer is "POST and GET" in an interview for the position of Web Application Developer. If you don't know what POST and GET are, you're really not Web Developer material. Still, I got the job after all that :)
  • (cs)

    And I can't express how much this forum / commenting software sucks. Sorry about the massive post guys ;^)

  • (cs)

    Well I don't know if anyone's reading these any more, but here's my 2p worth: sql solution to FizzBuzz

    declare @id int select @id = 1 while @id <= 100 begin select @id, substring(" Fizz Buzz FizzBuzz", ((1-sign(@id%3))+(1-sign(@id%5))* 2) * 8, 8)

    select @id = @id +1 end

  • (cs) in reply to Anonymous Scheme weenie, thinking of registering...
    Anonymous Scheme weenie:
    Here's a BF implementation, because I'm apparently _trying_ to make myself insane. If it actually worked (I doubt it does and I don't want to try it) it would zero-pad all the numbers, but it's pretty good otherwise. <snip for relatively obvious reasons/>
    Please, please register. Anybody who can code in Scheme and Brainfuck at the same time is an essential addition to this site. If you can manage to write a Visual Basic application that accesses a legacy Access database and scales to 10,000 users, that's cool too.

    Please, please register. I'll even buy you a pint of Theakston's Old Peculier...

  • (cs) in reply to JimM
    JimM:
    As an aside, I had an interview for a web developer on Friday, and at one point the tech interview started talking about web applications being stateless, and asked me how I would manage that state. I made a few nervous points about session variables / objects and data validation on the server side; he obviously wasn't getting what he wanted and prompted me about how to implement some state - so I suggested he was talking about AJAX and other asynchronous technologies. He still wasn't getting the reply he wanted, so he started talking at a more and more basic level about how you transfer the information, and I suddenly realised that he was asking if I knew how to use GET and POST to send information requests to the server. I couldn't decide what the biggest WTF was - him talking about that in terms of managing state, or him actually asking a question to which the answer is "POST and GET" in an interview for the position of Web Application Developer. If you don't know what POST and GET are, you're really not Web Developer material. Still, I got the job after all that :)
    Nor do any of the other "Web Developers," in my experience. Inverview fast. Interview often. Interview until your granny starts floating up the river.

    Then walk away. With dignity.

  • ais523 (unregistered) in reply to StarLite
    StarLite:
    <3 esoteric languages
    No, 3 esoteric languages so far (Befunge, BF, INTERCAL), and it is not true that 3<3.
  • Dan (unregistered) in reply to Otto
    Otto:
    Writing an O(n) algorithm for this problem is decidedly non-trivial. (Look up Boyer-Moore, it's pretty fancy.) I would not expect anyone to come up with it for an interview question without cheating, at least.

    Writing an efficient O(N) algorithm is hard, but even the general start from the front and match against each sub string is O(N). The worst case number of compares is N*m where N is the size of the string and m the size fo the substring.

    I think I would find a candidate interesting if they did even a subset of the more efficient algorithms.

  • (cs) in reply to real_aardvark
    real_aardvark:
    Please, please register. Anybody who can code in Scheme and Brainfuck at the same time is an essential addition to this site. If you can manage to write a Visual Basic application that accesses a legacy Access database and scales to 10,000 users, that's cool too.

    Please, please register. I'll even buy you a pint of Theakston's Old Peculier...

    Done. The promise of Old Peculier was too much to withstand.

    Also, I did once do an interview question in Scheme on a whiteboard. Then I was asked to do the same problem without using conditionals, so I wrote it in assembly. I am not making this up.

    But legacy Access databases? No sir, I still retain some ethics.

    ais523:
    No, 3 esoteric languages so far (Befunge, BF, INTERCAL), and it is not true that 3<3.

    http://xkcd.com/55/

  • Crehl (unregistered)

    I'm a few days late replying, who cares?

    I must either be missing something, or that guy truly is as stupid as he seems:

    string subtext = Console.ReadLine();
    [... Snip ...]
    System.Console.WriteLine(subtext.ToString() + " " + "Found, joy!" );
  • (cs) in reply to brian j. parker
    brian j. parker:
    London being a world city, I'd think you'd have the cream of the crop available.

    This was probably anonymized, but presumably from a comparable city. I wouldn't think that in any similar city you'd have trouble.

    You've never dealt with spotty twenty-three year old "interviewers" in London, have you?

    If not, be glad. London is a fetid dump. Avoid.

  • (cs) in reply to Air Hadoken
    Air Hadoken:
    real_aardvark:
    Please, please register. Anybody who can code in Scheme and Brainfuck at the same time is an essential addition to this site. If you can manage to write a Visual Basic application that accesses a legacy Access database and scales to 10,000 users, that's cool too.

    Please, please register. I'll even buy you a pint of Theakston's Old Peculier...

    Done. The promise of Old Peculier was too much to withstand.

    Also, I did once do an interview question in Scheme on a whiteboard. Then I was asked to do the same problem without using conditionals, so I wrote it in assembly. I am not making this up.

    But legacy Access databases? No sir, I still retain some ethics.

    ais523:
    No, 3 esoteric languages so far (Befunge, BF, INTERCAL), and it is not true that 3<3.

    http://xkcd.com/55/

    Well, I obviously owe you a pint. I'm quite impressed by your lack of moral fibre.

    Care to name a pub?

  • (cs) in reply to Anonymous Scheme weenie about to go to bed
    Anonymous Scheme weenie about to go to bed:
    <snip/>This may be premature, but I think I win.
    Even by trying, you lose.

    I don't suppose that this will stop the madness, unfortunately.

  • x (unregistered)
    #!/usr/bin/python
    
    import sys
    
    for i in xrange(1, 101):
            if i % 3 == 0:
                    sys.stdout.write("Fizz")
            if i % 5 == 0:
                    sys.stdout.write("Buzz")
            if i % 3 != 0 and i % 5 != 0:
                    sys.stdout.write("%d" % i)
    
            print
    

    Incredibly, that's what you have to do to print something out without an automatic newline.

  • (cs) in reply to real_aardvark
    real_aardvark:
    ... If not, be glad. London is a fetid dump. Avoid.

    Well we'll be delighted never to see you here then...

  • Noone (unregistered) in reply to Fanguad

    Actually, the most accurate response would be something along the lines of "The norm of the subtraction of the second point to the first point relative to the current inner product space". I believe it covers any sane problem involving distance between points/vector/functions/etc (And infinite-many insane ones too).

  • blazar (unregistered)

    In the Perl communities I'm in (NGs and fora) when such questions get posted they're generally recognized as homework and the OP is promptly asked to show what he's tried that far.

  • Alex (unregistered) in reply to Fanguad
    I once got asked "given two points, how would you find the distance between them?"

    My brain went into overdrive trying to find the hidden meaning behind the question. Were they talking three-dimensional points? Are the points moving? Are they some sort of weird hyper-points?

    I hesitantly responded, "The square root of delta x squared plus delta y squared?" The interviewer nodded and told me, "You'd be amazed at how many people have trouble with that."

    If I had asked you that question you'd damn well better give an answer that would work in arbitrary geometries.

    PS: x1 . g . x2

  • (cs) in reply to Noone
    Noone:
    Actually, the most accurate response would be something along the lines of "The norm of the subtraction of the second point to the first point relative to the current inner product space". I believe it covers any sane problem involving distance between points/vector/functions/etc (And infinite-many insane ones too).
    Are you absolutely sure that your answer covers the General Theory of Relativity, though?

    Answers on the back of a beer-mat to: Address anonymized.

  • (cs)

    Someone posted on this thread, linking to this website's article.

  • Weebs (unregistered)

    I don't even remember how I used to solve problems like this. Turn in code I've never even compiled? Shudder! These days I have to write a test, compile it, run it, and get a red bar before I can write any code at all.

    These days, if I interview a developer, I have a computer handy with Eclipse running on it, and I have him solve one or two problems from http://www.betterprogrammer.org while I pair with him. If he can't handle me watching over his shoulder, I'm not interested in him. If the first thing he does isn't write a test, I'm probably not interested in him.

  • Mike (unregistered) in reply to RF
    RF:
    ... I'm not sure whether it's a valid way of telling whether someone is 'smart' enough to work for you.

    No, but it's a valid way of telling whether someone is 'dumb' enough not to work for you.

    These questions look really easy to us but recall, the OPs said that their interviewers told them that many candidates failed.

    Lots of dumb people failing ==> pretty good filter. (I guess it's a "high pass" filter!)

  • Dan Fruzzetti (unregistered) in reply to Fanguad

    A very safe way to reply to this question that's insensitive to the confounding effects of "weird hyper-points" is to state it as:

    "the square root of the sum of the deltae squared for all individual dimensions."

    because, as you can understand by finding longest diagonals of any n-cube, it's all just the right-angle specialization of the pythagorean theorem.

    ~ dan ~

  • Yel (unregistered)

    I dont get it, why are all the codes here hard to read compared to mine. :( is this considered noob code? btw, how do you put tabs on the comments lol

    #include <iostream>

    using namespace std;

    int main() { char* input = "Polly sdf polly dfd polly dsssdsdspollya"; char* sub = "oll"; int i = 0, f = 0, newI = 0; bool isit = false; int inlength = strlen(input); int sublength = strlen(sub);

    while(true)
    {
    	if((i > inlength)|| (inlength < sublength) || (inlength - i < sublength) )break;
    		
        if(input[i] == sub[0])
    	{
            newI = i;
    		for(f = 0; f < sublength; f++)
    		{
    			if(input[newI++] != sub[f])
                {   
                    isit = false;
    				break;
                }
                isit = true;
    		}
    
            if(isit)
            { 
                cout << i << " ";                
            }
    	}
    	i++;
    }
    

    }

  • itsmo (unregistered) in reply to Dan Fruzzetti

    This 'distance between 2 points' thing - where the hell do you work that you have to think about sh!t like that?

  • steve (unregistered) in reply to joe_bruin
    joe_bruin:
    You're all fired. 2 comparisons worst case. ZERO division or modulo operators. 1/3 as many cycles in the loop.
    **snip**
    printf("fiz(%d)\n", i);
    **snip**
    I'm curious how you think printf can print an integer without any division or modulo. Is there an ASCII unsigned integer type that I don't know about?

    Anyway, that code doesn't print the numbers that are not divisible by 3 or 5, which is required by the traditional FizzBuzz problem. Here's my solution... the easy way.

    fizzbuzz.csv:

    "=IF(MOD(ROW(),3)*MOD(ROW(),5),TEXT(ROW(),""@""),CONCATENATE(IF(MOD(ROW(),3),"""",""Fizz""),IF(MOD(ROW(),5),"""",""Buzz"")))"
    **snip x99**

    Oh, is using Excel cheating?

    ...fine...

    0100:           MOV             AL,             01
                    MOV             BX,             0503
                    CMP             AL,             BH
                    JZ              0134
                    CMP             AL,             BL
                    JZ              0134
                    MOV             CH,             AL
                    CBW
                    MOV             CL,             0A
                    DIV             CL
                    MOV             DX,             AX
                    OR              DX,             3030
                    MOV             AH,             02
                    CMP             DL,             30
                    JZ              0123
                    INT             21
                    MOV             DL,             DH
                    INT             21
                    MOV             AH,             09
                    MOV             DX,             0178
                    INT             21
                    MOV             AL,             CH
                    INC             AL
                    JMP             0105
                    MOV             CX,             BX
                    CMP             BH,             BL
                    JB              0144
                    MOV             DX,             016E
                    INT             21
                    MOV             AL,             BL
                    ADD             CL,             03
                    CMP             BH,             BL
                    JA              0152
                    MOV             DX,             0173
                    INT             21
                    MOV             AL,             BH
                    ADD             CH,             05
                    INC             AL
                    MOV             DX,             0178
                    MOV             BL,             AL
                    INT             21
                    MOV             AL,             BL
                    MOV             BX,             CX
                    CMP             BH,             64
                    JBE             0105
                    CMP             BL,             64
                    JBE             0105
                    MOV             AX,             4C00
                    INT             21
                    DB              "Fizz$Buzz$", 0D, 0A, 24

Leave a comment on “Code examples and interviews”

Log In or post as a guest

Replying to comment #:

« Return to Article